r - 避免将 alpha 美学应用于 ggplot2 中的 geom_text

标签 r ggplot2

我有以下 ggplot2 图表。我不希望值(value)标签透明。

ggplot2 chart

代码:

ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level, alpha = round)) + 
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +
  scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) + 
  labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") +
  theme_bw() +
  geom_text(aes(label = round(obsAvg,1)), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) +
  scale_y_continuous(limits = c(0,4), expand = c(0,0)) +
  theme(legend.position="bottom")

数据:

set.seed(1)
test <- data.frame(
  org = rep(c("Mammals", "Cats", "Tigers", "Lions", "Cheetahs"), 3),
  level = rep(c("Animals", "Family", rep("Species", 3)), 3),
  group = rep("Cats",15),
  round = rep(c("Round1", "Round2", "Round3"),5),
  obsAvg = runif(15, 1, 4)
)

我曾尝试将 alpha = round 移动为 geom_bar() 的美学,但后来我失去了标签的闪避:

without dodge

如何复制顶部图表但不将透明美学应用于我的标签?

最佳答案

我会将 aes(alpha=) 移动到 geom_bar,然后将 aes(group=) 添加到 geom_text 恢复闪避。

ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level)) + 
  geom_bar(aes(alpha=round), stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +
  scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) + 
  labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") +
  theme_bw() +
  geom_text(aes(label = round(obsAvg,1), group=round), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) +
  scale_y_continuous(limits = c(0,4), expand = c(0,0)) +
  theme(legend.position="bottom")

enter image description here

这是一个漂亮的情节。

关于r - 避免将 alpha 美学应用于 ggplot2 中的 geom_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831535/

相关文章:

r - 使用 R 和术语文档矩阵创建频率表

r - 在运行时制作 R 函数 "listen"用于控制台输入?

r - 每行哪些列不适用

r - 如何绘制带有 NA 条目的线图?

r - Ggplot2 方面 : put y-axis of the right hand side panel on the right side

r - 重复 'row.names' 错误读数表。 row.names=NULL 移动列

r - 带有 setpmax 和阈值的神经网络包中出现错误

r - ggplot2 中的分组条

r - 覆盖 ggplot2 stat_密度2d 图,各组之间的 alpha channel 恒定

r - 将 ggplot 中堆积条形图的条从最小到最大排序