gpt4 book ai didi

r - 带有躲避条形图的 geom_text

转载 作者:行者123 更新时间:2023-12-04 07:35:46 27 4
gpt4 key购买 nike

我尝试添加文本,如 Position geom_text on dodged barplot
但它不适用于我的简单数据

data=data.frame(s=c(10,13,17,8),
pr=c("a","b","a","b"),
m=c(rep(as.Date('01.01.2015','%d.%m.%Y'),2), rep(as.Date('01.02.2015','%d.%m.%Y'),2)))

和 ggplot
ggplot(data = data 
,aes(x = m, y = s,fill=pr ,ymax = max(s)*1.1))+
geom_bar(position = "dodge",stat="identity")+
geom_text(aes(y=s/2,label=paste(round(s,3),"%")),position = position_dodge(width=1))+
scale_x_date(labels = date_format("%m/%y"),breaks = date_breaks("months"))

我明白了

enter image description here

如何在正确的位置(在每个条的中间)添加文本?
谢谢!

最佳答案

你可以试试

ggplot(data = data, aes(x = as.factor(m), y = s,fill=pr ,ymax = max(s)*1.1)) + 
geom_bar(position = "dodge", stat="identity") +
geom_text(aes(y=s/2,label=paste(round(s,3),"%")),position = position_dodge(.9)) +
scale_x_discrete(labels = function(x) format(as.Date(x), "%m/%y")) +
xlab("m")

关于r - 带有躲避条形图的 geom_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33079500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com