【问题标题】:Override alpha value for Geom_text in geom bar ggplot在 geom bar ggplot 中覆盖 Geom_text 的 alpha 值
【发布时间】:2020-11-10 09:47:02
【问题描述】:

这是我的代码;工作正常,除了来自 ggplot 的 alpha 滴流到 geom 文本标签。相反,我尝试通过再次指定 alpha 来覆盖它 - 不起作用。有什么解决方法吗?

p <- 
  labdata_N %>%
  ggplot(aes(x = reorder(type, measure), weight = measure, fill = type, alpha = Tobacco.Constituent)) +
  scale_alpha_manual(values = c(0.4, 0.6, 0.8, 1)) +
  geom_bar(position = "dodge", width = 0.75) +
  geom_text(aes(label=Tobacco.Constituent, y = measure), position=position_dodge(width=0.75), vjust=-0.25, size = 3) +
  scale_y_log10() +
  labs(x = "SLT type", y = "Nitrosamine level") +
scale_fill_manual(values=mycols,
labels=c(type)) +
theme(legend.position = "bottom",
      legend.title = element_blank(),
      axis.text = element_text(size=12),
      axis.title = element_text(size=14), 
      plot.title = element_text(size=14),
      legend.text = element_text(size=9),
      panel.background = element_rect()) +
annotation_logticks(side = "l") +
  guides(fill = FALSE)
print(p)

这是我的数据片段(如果您想要其他格式,请告诉我);谢谢,干杯!

1
NAB
HGT
60.56
2
NAB
HKH
375.01
3
NAB
HMH
3267.94
NAT
HGT
355.00
14
NAT
HKH
954.00
15
NAT
HMH
21062.00
NNK
HGT
229.62
26
NNK
HKH
249.87
27
NNK
HMH
10448.00
NNN
HGT
1303.00
38
NNN
HKH
3357.00
39
NNN
HMH
35603.00

【问题讨论】:

  • 为了让这成为一个更好的学习示例,您能否通过datapasta::dmdpaste 粘贴您的数据,或者改写ggplot 调用以使用例如默认的mtcars 数据集?

标签: r ggplot2 alpha geom-bar geom-text


【解决方案1】:

只需将alpha 美学映射移动到真正需要它的几何图形。喜欢

geom_bar(aes(alpha = Tobacco.Constituent))

“涓涓细流”是故意的,让美学被继承只是为了在下一步中覆盖它并没有多大意义..

【讨论】:

  • 太好了,谢谢!但后来我有些失去了我的 position_dodge;并且它们都堆积在同一条垂直线上而不是在右边的条上
  • 所以这是新代码,除了位置闪避,一切看起来都很好: p % ggplot(aes(x = reorder(type, measure), weight = measure)) + geom_col (aes(alpha = Tobacco.Constituent, y = measure, fill = type), position = "dodge", width = 0.75) + geom_text(aes(label=Tobacco.Constituent, y = measure), position=position_dodge(width= 0.75), vjust=-0.25, size = 3, check_overlap=T) + scale_alpha_manual(values = c(0.4, 0.6, 0.8, 1)) + scale_y_log10() +
  • 我已将最新的图片粘贴到主帖中;我现在唯一的问题是 position_dodge 不起作用
【解决方案2】:

使用 position = position_dodge2 代替 position = position_dodge 解决了最后一个问题。这是有效的代码 - 希望几年后有人可以使用它:

p <- 
  labdata_N %>%
Tobacco.Constituent)) +
  ggplot(aes(x = reorder(type, measure), weight = measure)) +
  geom_col(aes(alpha = Tobacco.Constituent, y = measure, fill = type), position = pos) +
  geom_text(aes(label=Tobacco.Constituent, y = measure), size = 3, position = position_dodge2(width = 0.75)) +
  scale_y_log10() +
  labs(x = "SLT type", y = "Nitrosamine level") +
scale_fill_manual(values=mycols,
labels=c(type)) +
theme(legend.position = "bottom",
      legend.title = element_blank(),
      axis.text = element_text(size=10),
      axis.title = element_text(size=10), 
      plot.title = element_text(size=10),
      legend.text = element_text(size=10),
      panel.background = element_rect()) +
annotation_logticks(side = "l") +
  guides(fill = FALSE)
print(p)

输出以图片形式发布; 谢谢@liborm ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多