geom_bar():堆叠和躲闪组合

时间:2014-07-18 08:25:07

标签: r ggplot2 geom-bar

我在ggplot2 :: geom_bar()上寻求建议。

我的数据包含类别" A"," B"," Z"。 我想要A,B作为堆积条,Z作为躲避栏旁边的A,B栏

# sample data
A = c(3, 4, 3, 5)
B = c(2, 2, 1, 4)
Z = c(1, 2, 1, 2)
R = c(-2, -1, -3, 0)
S = c(7,7,7,9)
mydata = data.frame(cbind(A,B,Z,R,S))
dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
mydata$date = as.Date(dates)
mydata.m = melt(mydata,id="date")
names(mydata.m) = c("variable", "category","value")



s = ggplot(mydata.m, aes(x=variable, fill=category)) +        
# bars for cat A, B, Z
geom_bar(data=subset(mydata.m, category %in% c("A","B")), aes(y=value), stat="identity", position="stack") +
geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity", position="dodge") +

# lines for cat R, S
geom_line(subset=.(category=="R"), aes(y=value), linetype="solid", size=1) + 
geom_line(subset=.(category=="S"), aes(y=value), linetype="solid", size=1) ;

以上并未将Z栏放在AB栏旁边,而是覆盖AB栏。使用aes(y = -value)或使用facet_wrap解决哪个问题,以便为" Z"制作单独的图。但我更喜欢 旁边的AB吧,而不是下面。有什么建议?谢谢!

附带问题:填充图例有R,S的颜色,但在图中线条只是黑色,我不明白为什么?

1 个答案:

答案 0 :(得分:2)

好的,所以几天前有一个good question有点类似于你所面临的根本问题,所以我要在这个问题的接受答案中使用该方法,因为它可以正常工作我不知道在stack中有dodge ed和ggplot2 d栏的任何其他方法。本质上,解决方案是手动移动感兴趣的geom_bar的数据。在上面引用的问题中,这更直接,因为水平轴是数字的,因此您可以快速确定要移动多少。在这种情况下,您的水平轴属于Date类,但不需要花费太多精力为category==Z数据子集找到合适的移位值。首先构建一个绘图对象,就像你上面所做的那样(我称之为s2);除了你知道你的两个geom_bar是重叠的,所以你必须稍微改变一下宽度 - 我在经过一些试验和错误之后使用了width = 5

s2 <- ggplot(
        mydata.m, 
        aes(x=variable, fill=category))+
      geom_bar(
        data=subset(mydata.m, category %in% c("A","B")), 
        aes(y=value), 
        stat="identity", 
        position="stack",
        width=5) +
     geom_bar(data = 
        subset(mydata.m, category=="Z"), 
        aes(y=-value), 
        stat="identity", 
        position="dodge",
        width=5)+ 
     geom_line(
        data = subset(mydata.m,category=="R"), 
        aes(y=value), 
        linetype="solid", 
        size=1) + 
     geom_line(
        data = subset(mydata.m,category=="S"), 
        aes(y=value), 
        linetype="solid", 
        size=1)
     ##
     s2

enter image description here

然后,我们可以通过

查看绘图对象的水平位置
> ggplot_build(s2)$panel$ranges[[1]]$x.major
    Jan 01     Jan 15     Feb 01     Feb 15     Mar 01     Mar 15     Apr 01 
0.06937799 0.20334928 0.36602871 0.50000000 0.63397129 0.76794258 0.93062201

我想你可以通过查看s2图上的x轴标签来观察这个,但是如果你想要计算特定的位置/坐标,这将给你详细信息。无论哪种方式,您只需要对要转移的数据进行一些调整,在这种情况下category == Z。我为此创建了一个新对象s3,我将数据移动了7(即7天,因为variableDate):

s3 <- ggplot(
  mydata.m, 
  aes(x=variable, fill=category))+
  geom_bar(
    data=subset(mydata.m, category %in% c("A","B")), 
    aes(y=value), 
    stat="identity", 
    position="stack",
    width=5) +
  geom_bar(
    subset(mydata.m, category=="Z"), 
    aes(y=value,x=variable+7), 
    stat="identity", 
    position="dodge",
    width=5)+ 
  geom_line(
    data = subset(mydata.m, category=="R"), 
    aes(y=value), 
    linetype="solid", 
    size=1) + 
  geom_line(
    data = subset(mydata.m, category=="S"), 
    aes(y=value), 
    linetype="solid", 
    size=1)
s3
##

enter image description here

编辑:另外,我不太确定为什么RS的行显示为黑色,但我怀疑它与你使用不同的事实有关他们geom。无论如何,你可以通过做

来解决这个问题
s5 <- ggplot(
  subset(mydata.m,
         !(category %in% c("R","S"))), 
  aes(x=variable, fill=category))+
  geom_bar(
    data=subset(mydata.m, category %in% c("A","B")), 
    aes(y=value), 
    stat="identity", 
    position="stack",
    width=5) +
  geom_bar(data = 
    subset(mydata.m, category=="Z"), 
    aes(y=value,x=variable+7), 
    stat="identity", 
    position="dodge",
    width=5)+ 
  geom_line(
    data=subset(
      mydata.m,
      category %in% c("R","S")), 
    aes(y=value,color=category), 
    linetype="solid", 
    size=1) + 
  geom_line(
    data=subset(
      mydata.m,
      category %in% c("R","S")), 
    aes(y=value,color=category), 
    linetype="solid", 
    size=1)
s5

enter image description here

这仍然会在原始图例框中显示RS(很可能是因为它们是因子category的因子级别),但您至少可以获得区分它们的另一个图例

相关问题