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

Sam*_*rke 3 r ggplot2

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

ggplot2 图表

代码:

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")
Run Code Online (Sandbox Code Playgroud)

数据:

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)
)
Run Code Online (Sandbox Code Playgroud)

我曾尝试将其转变alpha = round为一种美学geom_bar(),但后来我失去了标签的躲避:

不闪避

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

MrF*_*ick 5

我会移动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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是一个很漂亮的情节。

  • 另一种方法是通过“geom_text”中的“alpha = NULL”显式取消映射。 (4认同)