R-Studio中的堆叠Barplot

问题描述 投票:0回答:1

我正在尝试使用barplot()在R中建立一个堆叠的barplot。

我有一些类似的数据:

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

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

而且我想将其作为堆积的条形图,但是我得到的是:

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

enter image description here

可以通过某种方式在此处获得2条堆叠的条形吗?我已经尝试了很多,但是没有提出一个好的解决方案。 (我对R很陌生,并且不想使用ggplots等更高级别的绘图)。而且还有可能获得相对频率而不是绝对计数值吗?

预先感谢。

r statistics
1个回答
0
投票

也许您需要移调。

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)))
© www.soinside.com 2019 - 2024. All rights reserved.