我想要一些标签堆叠在geom_bar
图形。这是一个例子:
df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE)))
ggplot(df) + geom_bar(aes(x,fill=x)) + opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),axis.title.x=theme_blank(),legend.title=theme_blank(),axis.title.y=theme_blank())
Now
表(df$x)
FALSE TRUE
3 5
I'd like to have the 3 and 5 on top of the two bars. Even better if I could have the percent values as well. E.g. 3 (37.5%)
and 5 (62.5%)
. Like so:
(source: skitch.com https://img.skitch.com/20110623-xhbdwqj9bicqsf8nurg9dkuqsi.png)
这可能吗?如果是这样,怎么办?
在 a 上绘制文本ggplot
你使用geom_text
。但我发现首先使用总结数据很有帮助ddply
dfl <- ddply(df, .(x), summarize, y=length(x))
str(dfl)
由于数据是预先汇总的,因此您需要记住更改添加stat="identity"
参数为geom_bar
:
ggplot(dfl, aes(x, y=y, fill=x)) + geom_bar(stat="identity") +
geom_text(aes(label=y), vjust=0) +
opts(axis.text.x=theme_blank(),
axis.ticks=theme_blank(),
axis.title.x=theme_blank(),
legend.title=theme_blank(),
axis.title.y=theme_blank()
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)