使用基本R绘制分组和堆叠条形图

8
我希望创建一个组合条形图,在基本的R图形中,数据1和数据2堆叠在一起,而数据1和数据2之间是分组的。
类似于以下排列方式:

enter image description here

data1 <- matrix(c(1:5, rep(1,5), rep(4,5)), ncol=5)
data2 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5)

# stacked bar
barplot(data1)
#grouped var 
barplot(data1, beside=TRUE)

您有什么想法可以做到这一点吗?我知道我们可以使用不同的图表或方面来完成这个任务,但是我想在一个单独的图表中并排呈现它们。


7
可能是这样的: barplot(cbind(data1,data2),space=c(rep(0,ncol(data1)),2,rep(0,ncol(data2)))) 的意思是绘制一个并列条形图,其中 data1data2 是两个数据集,它们被合并在一起显示。space参数用于调整两组数据之间的间距。 - WaltS
2
@WaltS,你应该将这个作为答案发布。(但是要改成ncol(data2)-1以消除警告) - user20650
5个回答

4
您可以尝试使用mfrow将两个图形并排绘制:
par(mfrow = c(1,2))
barplot(data1, ylim = c(0,13), xlab = "data1")
barplot(data2, ylim = c(0,13), axes = FALSE, xlab = "data2")

enter image description here


4
您可以使用barplot的参数space移动第二个条形图
# plot first barplot, using xlim to ensure there is enough space to plot the second one 
# (here the spaces are all 0, so the bars are grouped together)
barplot(data1, space=0, xlim=c(-1, ncol(data1)+ncol(data2)+1), las=1)
# plot second barplot (with add=TRUE so the bars appear on the same plot), 
# putting a space of 6 (the number of columns of data1 + 1) at the beginning, 
# to plot the "new" bars on the right of the first bars (the other spaces are 0):
barplot(data2, space=c(ncol(data1)+1, rep(0, ncol(data2)-1)), las=1, add=TRUE)

enter image description here


1

我知道你正在寻找一个基于R的解决方案。这真是遗憾。ggplot2为此类任务提供了很好的可能性:

data3 = data.frame(data_nr = rep(c("data1","data2"), each=15),
                cat_1 = rep(rep(letters[1:5], each=3),2),
                cat_2 = rep(rep(LETTERS[1:3], 5),2),
                height = c(1:5, rep(1,5), rep(4,5), 2:6, rep(2,5), rep(3,5)))

ggplot(data3, aes(x=cat_1, y=height, fill=cat_2)) +
geom_bar(stat="identity", position="stack") +
facet_grid(~ data_nr)

提供:

输入图像说明

我可以想到一个纯基础R的(不太优雅的)解决方案,使用一些技巧:

data12 = cbind(data1, rep(NA,3), data2)
barplot(data12, space=0)

这段文本是HTML代码,其中包含一个图片链接。翻译成中文如下:

表示:

enter image description here

1
我知道ggplot2很棒,但我更喜欢基本解决方案,因为它有一些优点,比如我们不需要依赖包及其更新等。我尝试过单个图(而非多面板),但也无法实现。 - jon

1
这是一个自定义函数,您可以在其中操纵组间和组内图形,并且可以使用您想要绘制的任意数量的数据。
multistack.bar <- function(x=list(x), betweenspace = 2, withinspace=0, ...){
  # note that number of rows in each component matrix of the list
  # should be equal
  # if you have missing value use "NA", to make it complete 
  # number of column can differ
  mylist <- x
 space = list()
 space[[1]] <- c(rep(withinspace,ncol(mylist[[1]] )),betweenspace ) 

for ( i in 2:length(mylist)){
  if(i == length(mylist)){
  space[[i]] <- c(rep(withinspace,ncol(mylist[[i]] )-1)) 
  } else{
  space[[i]] <- c(rep(withinspace,ncol(mylist[[i]] )-1),betweenspace ) 
  }
  }
  un.space <- c(unlist(space))
  newdata <- do.call("cbind", mylist)
  barplot(newdata, space= un.space, ...)
 }

以下是三个数据集的示例。请注意,所有数据行数相同,但列数不同。由于行值被堆叠,因此应该是相同的数量,但可以通过为缺失的组放置NA或0来使行数相等。
data1 <- matrix(c(1:5, rep(1,5), rep(4,5)), ncol=5)
data2 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5)
data3 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5)
data4 <- matrix(c(1:4, rep(1,4), rep(4,4)), ncol=4)


mylist <- list(data1, data2, data3, data4)

multistack.bar(mylist, betweenspace=3, withinspace=0.1,
  col = c("pink", "blue", "purple"), xlab="groups", ylab="frequency", ylim = c(0, 16))

 # you can decrease space between bars to 0 no space and  between plots 
   multistack.bar(mylist, betweenspace=1, withinspace=0,
  col = c("pink", "blue", "purple"), xlab="groups", ylab="frequency", 
  ylim =    c(0, 16))


 # now you can use any thing you want just make it elegant or useful. 
    legend(8, 14.8, c("A", "B", "C"), fill = c("pink", "blue", "purple"), ncol=3 )
    abline(h=4, lty =2, col= "red")

enter image description here


-2
library(gridExtra)

data1 <- matrix(c(1:5, rep(1,5), rep(4,5)), ncol=5)
data2 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5)


# stacked bar
plot1 <- barplot(data1)
#grouped var 
plot2<- barplot(data1, beside=TRUE)

grid.arrange(plot1, plot2, ncol=2)

2
这对我似乎不起作用... barplot是一个基本绘图函数,而不是网格(还要查看plot1和plot2对象)。 - user20650
我不是很理解。 你需要使用库gridExtra, 而grid.arrange可以将两个图形并排放置。 - Bg1850
2
尝试运行你的代码...它不会工作...你会得到一个错误"...只允许grobs...". barplot是一个基本的r函数,实际上并没有返回图形(例如,像ggplot那样)。 - user20650
2
对我不起作用:给出以下错误信息 Error in gList(list(0.7, 1.9, 3.1, 4.3, 5.5, wrapvp = list(x = 0.5, y = 0.5, : only 'grobs' allowed in "gList" 此外,还有警告信息: 1: In grob$wrapvp <- vp : Coercing LHS to a list 2: In grob$wrapvp <- vp : Coercing LHS to a list - jon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接