覆盖Geom bar ggplot中Geom_文本的alpha值
这是我的密码;工作正常,除了ggplot中的alpha流到geom文本标签。相反,我试图通过再次指定alpha来覆盖它-不起作用。有解决办法吗覆盖Geom bar ggplot中Geom_文本的alpha值,r,ggplot2,alpha,geom-bar,geom-text,R,Ggplot2,Alpha,Geom Bar,Geom Text,这是我的密码;工作正常,除了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,
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)
只需将
alpha
美学贴图移动到真正需要的几何图形。像
geom_bar(aes(alpha = Tobacco.Constituent))
“涓涓细流”是有意为之的,让美感被继承下来只是为了在下一步覆盖它是没有多大意义的。使用position=position\u dodge 2代替position=position\u 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)
p%
烟草.成分)+
ggplot(aes(x=重新排序(类型、尺寸)、重量=尺寸))+
geom_col(aes(α=烟草成分,y=测量,填充=类型),位置=位置)+
几何图形文本(aes(标签=烟草成分,y=尺寸),尺寸=3,位置=位置2(宽度=0.75))+
比例尺10()+
实验室(x=“SLT类型”,y=“亚硝胺水平”)+
比例\填充\手动(值=mycols,
标签=c(类型))+
主题(legend.position=“bottom”,
legend.title=元素_blank(),
axis.text=元素\文本(大小=10),
axis.title=元素\文本(大小=10),
plot.title=元素\文本(大小=10),
legend.text=元素\文本(大小=10),
panel.background=element_rect())+
注释标记(side=“l”)+
辅助线(填充=假)
印刷品(p)
输出被张贴为pic;
谢谢@liborm 太好了,谢谢!但后来我又怎么丢了我的位置呢;所有这些都堆积在同一条垂直线上,而不是在右边的横条上。除了位置道奇,这里有新的代码,一切看起来都很好:p%ggplot(aes(x=重新排序(类型,度量),重量=度量))+geom_col(aes(alpha=烟草成分,y=度量,填充=类型),position=“道奇”,宽度=0.75)+geom_文本(aes(标签=烟草成分,y=测量值),位置=位置减淡(宽度=0.75),vjust=-0.25,尺寸=3,检查重叠=T)+比例-α-手动(值=c(0.4,0.6,0.8,1))+比例-y-对数10()+我已经将最新的图片粘贴到了主帖子中;我现在唯一的问题是道奇的位置,为了使这成为一个更好的学习示例,请您通过
DataPast::dmdpaste
粘贴数据,或者重新表述ggplot
调用以使用默认的mtcars
数据集?
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)