在R中创建单个条形图

时间:2017-09-28 15:40:39

标签: r ggplot2

我想在R

中绘制如下所示的条形图

enter image description here

这是从像

这样的数据框创建的
X1   X2  X3
No   No  No
Yes  No  Yes
Yes  Yes Yes

36% = total no of yes in X1
64% = (total no of yes in X2+total no of yes in X3)

如果问题不明确,请与我联系并发表评论。

1 个答案:

答案 0 :(得分:1)

获得它的直接方法是绘制多边形并添加文本。

Proportion1 = 0.36
Proportion2 = 0.64

plot(NULL, xlim=c(0,1), ylim=c(0,1), xaxt="n", yaxt="n",
    xlab="", ylab="", bty='n')
polygon(c(0,Proportion1,Proportion1,0), c(0.4,0.4,0.6,0.6), col="blue")
polygon(c(Proportion1,1,1,Proportion1), c(0.4,0.4,0.6,0.6), col="green")
text(c(Proportion1/2, Proportion2/2 + Proportion1), c(0.5,0.5), 
    c(paste0(100*Proportion1, "%"), paste0(100*Proportion2, "%")), 
    col="white", cex=1.5)

One Bar