ggplot2 条形图上的舍入百分比标签
q1 <- qplot(factor(Q1), data=survey, geom="histogram", fill=factor(Q1), ylim=c(0,300))
options(digits=2)
q1 + geom_bar(colour="black") +
stat_bin(aes(label=..count..), vjust=-2, geom="text", position="identity") +
stat_bin(geom="text", aes(label=paste(..count../sum(..count..)*100,"%"), vjust=-0.75)) +
labs(x="Question # 1:\n 0 = Didn't respond, 1 = Not at all familiar, 5 = Very familiar") +
opts(title="Histogram of Question # 1:\nHow familiar are you with the term 'Biobased Products'?",
legend.position = "none",
plot.title = theme_text(size = 16, , vjust = 1, face = "bold"),
axis.title.x =theme_text(size=14), axis.text.x=theme_text(size=12),
axis.title.y=theme_text(size=14, angle=90), axis.text.y=theme_text(size=12))
正如你所看到的,我得到的数字比需要的要多,我希望有选项( digits=2) 会这样做,但我想不会。有什么想法吗?
q1 <- qplot(factor(Q1), data=survey, geom="histogram", fill=factor(Q1), ylim=c(0,300))
options(digits=2)
q1 + geom_bar(colour="black") +
stat_bin(aes(label=..count..), vjust=-2, geom="text", position="identity") +
stat_bin(geom="text", aes(label=paste(..count../sum(..count..)*100,"%"), vjust=-0.75)) +
labs(x="Question # 1:\n 0 = Didn't respond, 1 = Not at all familiar, 5 = Very familiar") +
opts(title="Histogram of Question # 1:\nHow familiar are you with the term 'Biobased Products'?",
legend.position = "none",
plot.title = theme_text(size = 16, , vjust = 1, face = "bold"),
axis.title.x =theme_text(size=14), axis.text.x=theme_text(size=12),
axis.title.y=theme_text(size=14, angle=90), axis.text.y=theme_text(size=12))
As you can see I'm getting way more digits than what is needed, I was hoping the options(digits=2) would do it but I guess not. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其实你离那里很近。
这是一个最小的示例:
此外,还可以使用
format
、round
、prettyNum
等。更新:
感谢@Tommy的评论,这里有一个更简单的形式:
Actually you are very close to there.
Here is a minimal example:
also,
format
,round
,prettyNum
, etc, is available.UPDATED:
Thanks to @Tommy 's comment, here si a more simple form: