ggplot2:并排barplot与一个酒吧堆叠和其他不是 [英] ggplot2: side by side barplot with one bar stacked and the other not

查看:621
本文介绍了ggplot2:并排barplot与一个酒吧堆叠和其他不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为每个类别创建一个barplot,其中两个柱被绘制(并排):一个用于总计,另一个用子组堆叠。例如,在以下数据框中,名称将显示在x轴上。对于名称中的每个类别,一个栏将表示总计的值,另一栏将表示来自aaa,bbb和ccc的值的堆叠栏。我设法得到一个背对背的情节,但我不知道如何在这种情况下应用'闪避'的立场,以使酒吧并排。

  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), (data = subset(df,num!=total),aes(y = -values,fill = factor(num)),stat =身份,width = .5)
print(p)


解决方案 div>

您可以使用构面。看起来你不能同时堆叠和闪避(见下面的相关文章)。你可以在你的数据中添加另一个因子,用于x变量和名称变量的facet,以得到如下所示的结果:

编辑:调整棒的宽度以使棒接触评论。看到这里:



ggplot2 geom_bar position =" dodge"不闪避



绘制堆积条形图?



使用添加的地点因子编辑数据:

<$ p $ (名称=结构(c(1L,1L,1L,1L,2L,2L,2L,
2L,3L,3L,3L,3L) (1L,2L,3L,4L,1L,2L,3L,3L,3L,3L,3L,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)


I am trying to create a barplot where for each category, two bars are plotted (side by side): one is for the "total", the other is stacked by subgroups. For example, in the following data frame, 'names' will be shown on x-axis. For each category in 'names', one bar will represent the value of 'total', the other will be a stacked bar representing values from 'aaa', 'bbb' and 'ccc'. I managed to get a 'back-to-back' plot but I don't know how to apply the 'dodge' position to this case to make the bars side-by-side.

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)

解决方案

You can use facets. It seems you cannot stack and dodge at the same time (see related posts below). You can add another factor to your data for the x variable and facet on your names variable to come up with something like this:

Edit: Adjusted width of bars to have bars touching as per comments. See here: 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

It looks like you can adjust the margins of the facets to make the ABC groups look closer together if you're interested. For some examples, see these related posts:

ggplot2 - bar plot with both stack and dodge

ggplot2 geom_bar position = "dodge" does not dodge

Plotting a stacked bar plot?

Edited data with added "place" factor:

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:并排barplot与一个酒吧堆叠和其他不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆