我如何显示条形内水平堆叠的小号的值?
我有一个使用此代码创建的堆叠条图,
p<- ggplot(data = df21[1:20,], aes(x =Initiative, y = Frequency, fill= Topic)) +
geom_col(aes(fill = Topic), width = 0.5)+
geom_text(aes(label = Frequency, #paste0(round(100*value/sum(value),2),"%")
),
vjust = -1.5,
hjust = 6,
color = "black",
position ="stack", #position_dodge(preserve = "single"),
size = 5)+
theme_classic()+
coord_flip()+
scale_y_continuous(breaks=seq(0, 100, by=10))+
geom_bar(position="stack", stat="identity", width = .5)+
scale_x_discrete(labels = function(x) str_wrap(x, width = 35))+
theme(legend.position="bottom",
axis.text.x = element_text( face = "plain", size = 15, vjust= 0.5),
axis.text.y = element_text( face = "plain", size = 15, vjust= 0.5),
plot.title = element_text(family = "Arial", face = "bold", size = (18),colour = "steelblue4"),
legend.title = element_text( face = "bold", size = 15, vjust= 1, hjust =-3),
legend.text = element_text(face = "bold",
colour = "steelblue4", family = "Arial", size = 8),
legend.key.size = unit(0.8, 'cm'), #change legend key size
legend.key.height = unit(0.8, 'cm'), #change legend key height
legend.key.width = unit(0.8, 'cm'),
axis.title = element_text(face = "bold",family = "Arial", size = 15, colour = "steelblue4") )+
labs(title = "Topics per Initiative", y = "Counts per Topic", x = "Initiative")+
guides(fill=guide_legend(nrow=6, byrow=TRUE,
shape = guide_legend(override.aes = list(size = 6)),
))
我一直在玩Hjust和vjust,并设法将数字放在上面,否则它们会出现在这样的情节后面。
我一直在寻找解决方案并播放代码!我需要帮助!我真的很想在每个“堆叠”栏的顶部和中心都有这些数字。谢谢!
I have a stacked bar plot i created with this code
p<- ggplot(data = df21[1:20,], aes(x =Initiative, y = Frequency, fill= Topic)) +
geom_col(aes(fill = Topic), width = 0.5)+
geom_text(aes(label = Frequency, #paste0(round(100*value/sum(value),2),"%")
),
vjust = -1.5,
hjust = 6,
color = "black",
position ="stack", #position_dodge(preserve = "single"),
size = 5)+
theme_classic()+
coord_flip()+
scale_y_continuous(breaks=seq(0, 100, by=10))+
geom_bar(position="stack", stat="identity", width = .5)+
scale_x_discrete(labels = function(x) str_wrap(x, width = 35))+
theme(legend.position="bottom",
axis.text.x = element_text( face = "plain", size = 15, vjust= 0.5),
axis.text.y = element_text( face = "plain", size = 15, vjust= 0.5),
plot.title = element_text(family = "Arial", face = "bold", size = (18),colour = "steelblue4"),
legend.title = element_text( face = "bold", size = 15, vjust= 1, hjust =-3),
legend.text = element_text(face = "bold",
colour = "steelblue4", family = "Arial", size = 8),
legend.key.size = unit(0.8, 'cm'), #change legend key size
legend.key.height = unit(0.8, 'cm'), #change legend key height
legend.key.width = unit(0.8, 'cm'),
axis.title = element_text(face = "bold",family = "Arial", size = 15, colour = "steelblue4") )+
labs(title = "Topics per Initiative", y = "Counts per Topic", x = "Initiative")+
guides(fill=guide_legend(nrow=6, byrow=TRUE,
shape = guide_legend(override.aes = list(size = 6)),
))
it creates this plot with the number of counts for each stack on top.
i have been playing with hjust and vjust and manage to get the numbers on top, otherwise they appear behind the plot like this.
I've been looking for solutions and playing around with the code! i need help! i really would like to have the numbers on top and in the center of each " stacked" bar. thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用
vjust
或hjust
在绘制文本时,建议您计算每个标签的确切位置,并将其绘制为y Anesthetic
。这将使图看起来更优雅,并且当值更改时不需要手动调整。在2022-05-23上由 reprex软件包(v2.0.1)
Instead of using
vjust
orhjust
when drawing text I recommend you calculate the exact position of each label and drawing them using that as they aesthetic
. This will make the graph look more elegant and doesn't need manual adjustment when values change.Created on 2022-05-23 by the reprex package (v2.0.1)