R 避免将alpha美学应用于ggplot2中的geom_文本

R 避免将alpha美学应用于ggplot2中的geom_文本,r,ggplot2,R,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")) +

我有以下
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")
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)
)
数据:

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)
)
set.seed(1)
测试我会将
aes(alpha=)
移动到
geom_条
,然后将
aes(group=)
添加到
geom_文本
以恢复躲避

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")


这是一个漂亮的图。

另一种方法是通过
geom\u text
中的
alpha=NULL
显式取消映射。此解决方案非常有效,谢谢。正如joran的评论一样,将
group=round
alpha=NULL
添加到
geom_文本
中,其余部分保持原样。