% g-6ren">
gpt4 book ai didi

r - `geom_text()` 标签非常浅/微弱 - 需要它们正常/深色

转载 作者:行者123 更新时间:2023-12-02 15:45:46 25 4
gpt4 key购买 nike

# Create the data frame
library(tidyverse)
dat <- read.table(text = "A B C
1 23 234 324
2 34 534 120
3 56 324 124
4 34 234 124
5 123 534 654",
sep = "",
header = TRUE) %>%
gather(key = "variable", value = "value") %>%
group_by(variable) %>%
mutate(ind = as.factor(rep(1:5)),
perc = value / sum(value)) %>%
arrange(variable, -perc) %>%
mutate(ordering = row_number()) %>%
mutate(lab.y = cumsum(perc),
lab.y.mid = lab.y - (perc / 2))

# Toggle whether red is on top/bottom with '1L' or '-1L'
red <- 1L
n_ord <- length(unique(dat$ordering))
fill_scale <- c("darkred", rep("black", n_ord - 1L)) %>%
setNames(red * seq(n_ord))
alpha_scale <- c(0.5, rep(0.3, n_ord - 1L)) %>%
setNames(red * seq(n_ord))
# Plot the data
ggplot(dat, aes(variable,
perc,
fill = factor(red * ordering),
alpha = factor(red * ordering))) +
geom_col(color = "white", size = 1.5) +
scale_fill_manual(guide = "none", values = fill_scale) +
scale_alpha_manual(guide = "none", values = alpha_scale) +
facet_grid(~ variable, scales = "free_x") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) +
geom_text(aes(y = 1 - lab.y.mid, label = ind), color = "black")

light text on plot

我一直假设 ggplot 按顺序、逐行项目地绘制事物。上面我的 ggplot 的最后一行是:

geom_text(aes(y = 1 - lab.y.mid, label = ind), color = "black")

但这个命令似乎并不是 ggplot“做”的最后一件事。如果您查看上面的图,您会发现我的文本标签非常微弱。文本要么位于情节的某些部分的后面,要么继承了某种类型的 alpha 级别,或者发生了其他我没有想到的事情。

如何让文本变暗(就像平常一样)?就像下面这个图一样。

dark crisp clear text on graph

最佳答案

geom_text 继承了 ggplot()alpha 美学,这就是文本不显示为“黑色”的原因。

将最后一行更改为

...  + 
geom_text(aes(x = variable, y = 1 - lab.y.mid, label = ind), inherit.aes = FALSE)

得到这个结果

enter image description here

<小时/>

另一种选择是覆盖 alpha

... +
geom_text(aes(y = 1 - lab.y.mid, label = ind), alpha = 1)

关于r - `geom_text()` 标签非常浅/微弱 - 需要它们正常/深色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54333718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com