ggplot2 facetting with multiple layers

时间:2016-03-28 21:39:28

标签: r ggplot2 facet

在单个地块上覆盖两个geoms时,我无法获得正确的刻面行为。

我的输入数据包含两个数据帧。 df包含按variable分组并按short_taxa分组的分类单元丰度数据,然后由SampleType进行分层。

df <- structure(list(variable = c("Subject1", "Subject1", "Subject2", 
"Subject2", "Subject5", "Subject5", "Subject7", "Subject7", "Subject8", 
"Subject8", "Subject11", "Subject11", "Subject12", "Subject12", 
"Subject14", "Subject14", "Subject15", "Subject15", "Subject18", 
"Subject18", "Subject20", "Subject20", "Subject22", "Subject22", 
"Subject24", "Subject24", "Subject25", "Subject25", "Subject28", 
"Subject28", "Subject30", "Subject30", "Subject31", "Subject31", 
"Subject32", "Subject32"), value = c(32.4137931034483, 0, 13.6363636363636, 
19.435736677116, 16.3304514889529, 27.4735830931796, 38.4180790960452, 
0.564971751412429, 33.9024390243902, 1.46341463414634, 37.2093023255814, 
5.42635658914729, 23.3175355450237, 32.7962085308057, 35.8024691358025, 
0, 14.1210374639769, 48.4149855907781, 1.86516853932584, 1.50561797752809, 
43.9490445859873, 0.955414012738854, 3.76932989690722, 88.8208762886598, 
29.6511627906977, 4.36046511627907, 32.4742268041237, 19.5876288659794, 
2.03570310053241, 91.1055433761353, 40.9356725146199, 0, 31.2335958005249, 
6.2992125984252, 35.1084812623274, 7.88954635108481), short_taxa = c("f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae", "f__Retroviridae", 
"f__Siphoviridae", "f__Retroviridae", "f__Siphoviridae"), SampleType = c("Group2", 
"Group2", "Group3", "Group3", "Group1", "Group1", "Group2", "Group2", 
"Group3", "Group3", "Group2", "Group2", "Group1", "Group1", "Group2", 
"Group2", "Group3", "Group3", "Group1", "Group1", "Group2", "Group2", 
"Group3", "Group3", "Group3", "Group3", "Group1", "Group1", "Group1", 
"Group1", "Group2", "Group2", "Group3", "Group3", "Group1", "Group1"
)), .Names = c("variable", "value", "short_taxa", "SampleType"
), row.names = c(17L, 21L, 43L, 47L, 121L, 125L, 173L, 177L, 
199L, 203L, 277L, 281L, 303L, 307L, 355L, 359L, 381L, 385L, 459L, 
463L, 511L, 515L, 563L, 567L, 615L, 619L, 641L, 645L, 719L, 723L, 
771L, 775L, 797L, 801L, 823L, 827L), class = "data.frame")

我可以很好地得到这样的情节:

ggplot(df, aes(x=variable, y=value, fill=short_taxa, group=short_taxa))
  + geom_bar(stat="identity", position="stack") + ylim(c(-10, 100)) 
  + facet_wrap(~SampleType, scales="free") + theme_classic() + 
  theme(legend.position = "right", legend.key.size = unit(1, "lines"),
  axis.text.x = element_text(angle=90, vjust=0.5), plot.margin = 
  unit(c(2, 1, 0.5, 0.5), "lines"))

enter image description here

现在我要做的是在每个堆积的条形图(对应于每个geom_rect)下方添加Subject,表示该主题的密度测量值。

df.coloring <- structure(list(variable = c("Subject24", "Subject25", "Subject7", 
"Subject28", "Subject29", "Subject13", "Subject9", "Subject32", 
"Subject33", "Subject11", "Subject20", "Subject14", "Subject21", 
"Subject5", "Subject1", "Subject17", "Subject18", "Subject3"), 
    xmin = c(0.5, 1.5, 4.5, 6.5, 7.5, 10.5, 11.5, 13.5, 14.5, 
    17.5, 19.5, 21.5, 23.5, 24.5, 27.5, 29.5, 30.5, 31.5), xmax = c(1.5, 
    2.5, 5.5, 7.5, 8.5, 11.5, 12.5, 14.5, 15.5, 18.5, 20.5, 22.5, 
    24.5, 25.5, 28.5, 30.5, 31.5, 32.5), ymin = c(-6, -6, -6, 
    -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6
    ), ymax = c(-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, 
    -4, -4, -4, -4, -4, -4), SampleType = c("Group2", "Group3", 
    "Group1", "Group2", "Group3", "Group2", "Group1", "Group2", 
    "Group3", "Group1", "Group2", "Group3", "Group3", "Group1", 
    "Group1", "Group2", "Group3", "Group1"), density = c(0.640242130728438, 
    0.116821877425537, 0.0310043091885746, 0.0189890721812844, 
    0.974712340626866, 0.421599371824414, 0.169613848207518, 
    0.76187791978009, 0.69058098597452, 0.600862825522199, 0.671995443990454, 
    0.225653737317771, 0.911656582495198, 0.342635749839246, 
    0.138989825500175, 0.987418259494007, 0.739982327679172, 
    0.241753033129498)), .Names = c("variable", "xmin", "xmax", 
"ymin", "ymax", "SampleType", "density"), row.names = c(1L, 2L, 
5L, 7L, 8L, 11L, 12L, 14L, 15L, 18L, 20L, 22L, 24L, 25L, 28L, 
30L, 31L, 32L), class = "data.frame")

我尝试了各种迭代:

ggplot(df, aes(x=variable, y=value, fill=short_taxa,  order=short_taxa)) 
  + geom_bar(stat="identity", position="stack") + ylim(c(-10, 100)) 
  + geom_rect(data=df.coloring, aes(xmin=xmin, xmax=xmax, ymin=ymin, 
    ymax=ymax, color=density), fill=NA, inherit.aes=F)
  + scale_color_distiller() + facet_wrap(~SampleType, scales="free") 
  + theme_classic() + theme(legend.position = "right", 
    legend.key.size = unit(1, "lines"), axis.text.x = 
    element_text(angle=90, vjust=0.5), plot.margin = 
    unit(c(2, 1, 0.5, 0.5), "lines"))

但最终发生的事情是每个SampleType绘制所有18个主题,而不仅仅是属于小平面的6个主题。 enter image description here

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:3)

我认为这种情况geom_tile的效果会优于geom_rect

从帮助页面:

  

geom_rect使用四个角的位置(xmin,xmax,ymin和   YMAX)。

     

geom_tile使用图块的中心及其大小(x,y,宽度,   高度)。

通过将切片置于variable的中心,您的切片应该更好用,因为geom_bargeom_tile的x轴将基于相同的变量。要使用geom_tile,您需要定义ywidthheight。您可以将它们放入df.coloring而不是yminymax等。

df.coloring$y = -5
df.coloring$height = 2
df.coloring$width = 1

在旁注中,variable中的df.coloring值似乎与SampleType无法正确匹配(与df中的内容相比)。这使事情变得更加复杂。为了得到我所做的示例代码的正确顺序:

df.coloring$variable = unique(df$variable)

现在使用geom_tile代替geom_rect来获取您想要的情节:

ggplot(df, aes(x=variable, y=value, fill=short_taxa,  order=short_taxa)) + 
    geom_bar(stat="identity", position="stack") + 
    ylim(c(-10, 100)) + 
    geom_tile(data = df.coloring, aes(x = variable, y = y, width = width, 
                                    height = height, color = density), 
              fill = NA, inherit.aes = FALSE) + 
    scale_color_distiller() + 
    facet_wrap(~SampleType, scales="free_x") + 
    theme_classic() + 
    theme(legend.position = "right",
          legend.key.size = unit(1, "lines"), 
          axis.text.x = element_text(angle=90, vjust=0.5), 
          plot.margin = unit(c(2, 1, 0.5, 0.5), "lines"))

enter image description here

由于您希望所有图块的大小相同,因此您可以为ywidth等提供固定值,而不是将其添加到数据集中。

geom_tile(data=df.coloring, aes(x = variable, y = -5, width = 1, 
                                        height = 2, color=density), 
                  fill = NA, inherit.aes = FALSE)
相关问题