简体   繁体   English

在 R 中创建单个条形图

[英]Creating a single bar chart in R

I want to draw a bar graph like shown below in R我想在 R 中绘制如下所示的条形图

在此处输入图片说明

This is created from data frame like这是从数据框创建的

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)

Please bear with me and comment if the question is not clear.如果问题不清楚,请耐心等待并发表评论。

A direct way to get that is to just plot the polygons and add the text.获得它的直接方法是绘制多边形并添加文本。

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)

一酒吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM