R:有两组的条形图,其中一组是堆叠的

时间:2022-11-16 23:40:09

I've encountered a small problem when creating a bar plot in R. There are 3 variables:

我在R中创建条形图时遇到了一个小问题。有3个变量:

a <- c(3,3,2,1,0)
b <- c(3,2,2,2,2)
c <- 0:4

The bar plot should be grouped by 'a' and 'c', and 'b' should be stacked on top of 'a'. Doing the grouping and stacking seperately is straightforward:

条形图应按“a”和“c”分组,“b”应堆叠在“a”的顶部。单独进行分组和堆叠非常简单:

barplot(rbind(a,c), beside=TRUE)
barplot(rbind(a,b), beside=FALSE)

How can you do both at once in one graph?

如何在一个图表中一次完成两个操作?

2 个解决方案

#1


10  

Doing this requires thinking about how barplot draws stacked bars. Basically, you need to feed it some data with 0 values in appropriate places. With your data:

这样做需要考虑条形图如何绘制堆积条。基本上,您需要在适当的位置为其提供一些具有0值的数据。使用您的数据:

mydat <- cbind(rbind(a,b,0),rbind(0,0,c))[,c(1,6,2,7,3,8,4,9,5,10)]
barplot(mydat,space=c(.75,.25))

R:有两组的条形图,其中一组是堆叠的

To see what's going on under the hood, take a look at mydat:

要了解幕后发生的事情,请查看mydat:

> mydat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
a    3    0    3    0    2    0    1    0    0     0
b    3    0    2    0    2    0    2    0    2     0
     0    0    0    1    0    2    0    3    0     4

Here, you're plotting each bar with three values (the value of a, the value of b, the value of c). Each column of the mydat matrix is a bar, sorted so that the ab bars are appropriately interspersed with the c bars. You may want to play around with spacing and color.

在这里,您将使用三个值绘制每个条形图(a的值,b的值,c的值)。 mydat矩阵的每一列都是一个条形,进行排序,以便ab条适当地散布在c条中。您可能想要使用间距和颜色。

Apparently versions of this have been discussed on R-help various times without great solutions, so hopefully this is helpful.

显然这个版本已经在R-help上进行过多次讨论而没有很好的解决方案,所以希望这很有帮助。

#2


0  

Try the lattice lib:

尝试格子库:

library("lattice")
MyData <- as.data.frame(Titanic)

barchart(Freq ~ Survived | Age * Sex, groups = Class, data = MyData,
         auto.key = list(points = FALSE, rectangles = TRUE, space
         = "right", title = "Class", border = TRUE), xlab = "Survived",
         ylim = c(0, 800))

R:有两组的条形图,其中一组是堆叠的

As you can see the grouping and ploting is done at once.

如您所见,分组和绘图是立即完成的。

Please also see: https://stat.ethz.ch/pipermail/r-help/2004-June/053216.html

另请参阅:https://stat.ethz.ch/pipermail/r-help/2004-June/053216.html

#1


10  

Doing this requires thinking about how barplot draws stacked bars. Basically, you need to feed it some data with 0 values in appropriate places. With your data:

这样做需要考虑条形图如何绘制堆积条。基本上,您需要在适当的位置为其提供一些具有0值的数据。使用您的数据:

mydat <- cbind(rbind(a,b,0),rbind(0,0,c))[,c(1,6,2,7,3,8,4,9,5,10)]
barplot(mydat,space=c(.75,.25))

R:有两组的条形图,其中一组是堆叠的

To see what's going on under the hood, take a look at mydat:

要了解幕后发生的事情,请查看mydat:

> mydat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
a    3    0    3    0    2    0    1    0    0     0
b    3    0    2    0    2    0    2    0    2     0
     0    0    0    1    0    2    0    3    0     4

Here, you're plotting each bar with three values (the value of a, the value of b, the value of c). Each column of the mydat matrix is a bar, sorted so that the ab bars are appropriately interspersed with the c bars. You may want to play around with spacing and color.

在这里,您将使用三个值绘制每个条形图(a的值,b的值,c的值)。 mydat矩阵的每一列都是一个条形,进行排序,以便ab条适当地散布在c条中。您可能想要使用间距和颜色。

Apparently versions of this have been discussed on R-help various times without great solutions, so hopefully this is helpful.

显然这个版本已经在R-help上进行过多次讨论而没有很好的解决方案,所以希望这很有帮助。

#2


0  

Try the lattice lib:

尝试格子库:

library("lattice")
MyData <- as.data.frame(Titanic)

barchart(Freq ~ Survived | Age * Sex, groups = Class, data = MyData,
         auto.key = list(points = FALSE, rectangles = TRUE, space
         = "right", title = "Class", border = TRUE), xlab = "Survived",
         ylim = c(0, 800))

R:有两组的条形图,其中一组是堆叠的

As you can see the grouping and ploting is done at once.

如您所见,分组和绘图是立即完成的。

Please also see: https://stat.ethz.ch/pipermail/r-help/2004-June/053216.html

另请参阅:https://stat.ethz.ch/pipermail/r-help/2004-June/053216.html