ggplot2:并排条形图,一个条堆叠,另一个不堆叠

ggplot2: side by side barplot with one bar stacked and the other not

我正在尝试创建一个条形图,其中为每个类别绘制了两个条形图(并排):一个用于 "total",另一个由子组堆叠。例如,在下面的数据框中,'names' 将显示在 x 轴上。对于 'names' 中的每个类别,一个条将代表 'total' 的值,另一个将是一个堆叠条,代表 'aaa'、'bbb' 和 'ccc' 的值.我设法得到了一个 'back-to-back' 图,但我不知道如何将 'dodge' 位置应用于这种情况以使条形并排。

df = data.frame(names = rep(LETTERS[1:3], each=4), 
                num = c(rep(c("aaa","bbb","ccc","total"), 3)), 
                values = c(1,2,3,7,2,2,5,10,3,4,2,9)))
p = ggplot(df, aes(x=factor(names))) + 
    geom_bar(data=subset(df,num=="total"), aes(y=values), stat="identity",width=.5) +
    geom_bar(data=subset(df,num!="total"), aes(y=-values,fill=factor(num)), stat="identity",width=.5) 
print(p)

您还可以增加条形的宽度以更好地贴合图形。 试试这个:

 p = ggplot(df, aes(x=factor(names)), ) + 
  geom_bar(width=0.75,data=subset(df,num=="total"), aes(y=values), stat="identity",width=.5) +
  geom_bar( width=0.75, data=subset(df,num!="total"), aes(y=-values,fill=factor(num)), stat="identity",width=.5) 
print(p)

编辑:

我想我误解了你的问题。你想让一个名字中的横条并排吗?

您可以使用分面。看来你不能同时堆叠和闪避(见下面的相关帖子)。您可以为 x 变量和 names 变量的方面的数据添加另一个因素,以得出如下结果:

编辑:调整条形宽度,使条形根据评论相互接触。参见此处:Remove space between bars ggplot2.

library(ggplot2)
p <- ggplot(data = df, aes(x = place, y = values, colour = num, fill = num))
p <- p + geom_bar(stat = "identity", width = 1, position = "stack")
p <- p + facet_grid(. ~ names)
p

如果您有兴趣,您似乎可以调整分面的边距,使 ABC 组看起来靠得更近。有关一些示例,请参阅这些相关帖子:

ggplot2 - bar plot with both stack and dodge

ggplot2 geom_bar position = "dodge" does not dodge

Plotting a stacked bar plot?

添加了 "place" 因素的编辑数据:

df <-    structure(list(names = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    num = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L), .Label = c("aaa", "bbb", "ccc", "total"), class = "factor"), 
    values = c(1, 2, 3, 7, 2, 2, 5, 10, 3, 4, 2, 9), position = structure(c(1L, 
    1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L), .Label = c("nums", 
    "total"), class = "factor")), .Names = c("names", "num", 
"values", "place"), row.names = c(NA, -12L), class = "data.frame")

我相信自发布这些答案以来,ggplot2 包已经发生了变化。我正在使用 R 版本 4.0.2 和 ggplot2 版本 3.3.3。如果我按照你的要求去做,我相信 stat='identity'position=position_dodge() 函数中的参数应该符合你的要求,至少对我来说是这样。

  ggplot(df, aes(names, values, fill=num))+
     geom_bar(stat='identity', position = position_dodge())