Stacked Barplot in R-Studio

huangapple go评论85阅读模式
英文:

Stacked Barplot in R-Studio

问题

我正在尝试使用barplot()函数在R中构建一个堆叠的条形图。

我有一些数据如下:

counts1 = table(questions$first)
counts1
0  1  
29 81 

counts2 = table(questions$snd)
counts2
 0  1  
40 48

我想要将它们作为堆叠的条形图呈现,但我得到的是:

barplot(c(counts1, counts2), main="BarPlot",
        col=c("darkblue", "red"), horiz=TRUE)

Stacked Barplot in R-Studio

是否有可能在这里获得两个堆叠的条形图?我尝试了很多但没有找到一个好的解决方案(我对R相当新,不想使用更高级别的绘图库如ggplot)。此外,是否可以获得相对频率而不是绝对计数值?

提前感谢。

英文:

I am trying to build up a stacked barplot in R using barplot().

I have some Data given like :

counts1= table(questions$first)
counts1
0  1  #output
29 81 

counts2 = table(questions$snd)
counts2
 0  1 
40 48

And I would like to have that as an stacked barplot, but what I get is :

barplot(c(counts1,counts2), main="BarPlot",
        col=c("darkblue","red"),horiz = TRUE)

Stacked Barplot in R-Studio

is it somehow possible to get 2 stacked bars here? I have tried a lot but I did not come up with a good solution. (I am quite new to R and dont want to use higher level plots like ggplots). And furthermore is it possible to get the relative frequency instead of absolut count values?

Thanks in Advance.

答案1

得分: 1

代码部分不需要翻译。

英文:

Perhaps you need to transpose.

counts1 <- read.table(text="0  1  
29 81", header=TRUE) 
counts2 <- read.table(text="0  1  
40 48", header=TRUE) 

barplot(t(rbind(counts1=counts1, counts2=counts2)))

Stacked Barplot in R-Studio

huangapple
  • 本文由 发表于 2020年1月3日 22:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定