将值添加到R中表的条形图中 [英] Adding values to barplot of table in R
本文介绍了将值添加到R中表的条形图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制带有barplot的表并向其中添加值.
I am trying to plot a table with barplot and adding values to it.
tt = structure(c(7L, 13L, 24L, 30L, 30L, 38L, 35L, 45L, 37L,
43L, 38L, 59L, 33L, 45L, 37L, 58L), .Dim = c(2L, 8L), .Dimnames = structure(list(param = c("A",
"B"), xvar = c("5", "6", "7", "8", "9", "10", "11", "12")), .Names = c("param", "xvar")), class = "table")
tt
xvar
param 5 6 7 8 9 10 11 12
A 7 24 30 35 37 38 33 37
B 13 30 38 45 43 59 45 58
bb= barplot(tt)
text(bb, 0, tt)
并且:
bb= barplot(tt)
text(bb, tt, tt)
两者均未正确放置值.我也在text()中尝试过t(tt),但无法正常工作.如何才能做到这一点.感谢您的帮助.
Both do not put values properly. I also tried t(tt) in text() but it does not work properly. How can this be done. Thanks for your help.
推荐答案
嗯,这行得通,但是我不得不认为还有更好的方法...
Well, this works, but I have to think there's a better way...
bb <- barplot(tt, col=c("grey60","grey80"))
text(bb,tt[1,]-4,labels=tt[1,],cex=.8)
text(bb,colSums(tt)-4,labels=tt[2,],cex=0.8)
这是一个ggplot
解决方案,仅出于完整性考虑.
And here's a ggplot
solution, just for completeness.
library(ggplot2)
ggplot(as.data.frame(tt),aes(x=factor(xvar),y=Freq,fill=param)) +
geom_bar(stat="identity",position="stack")+
geom_text(aes(label=Freq),position="stack",vjust=1)+
scale_fill_manual(values=c("grey60","grey80"))+
theme_bw()
这篇关于将值添加到R中表的条形图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文