如何在ggplot2中的堆叠条组之间放置空格

时间:2013-05-17 02:01:25

标签: r ggplot2

我试图在空间上成对分组堆叠,使得0W处理接近相应的6W处理,在不同处理对之间具有更大的空间。我已经包含了一个列“组”来显示哪些对应该组合在一起。

数据:

       trt            org   time  copy     group
1    Notill_D0 0W    fungi   0W 9.78e+07     a
2  Notill_D707 0W    fungi   0W 1.55e+07     b
3    Native_D0 0W    fungi   0W 4.02e+07     c
4  Native_D707 0W    fungi   0W 1.04e+07     d
5    Notill_D0 6W    fungi   6W 5.51e+07     a
6  Notill_D707 6W    fungi   6W 1.43e+07     d
7    Native_D0 6W    fungi   6W 1.60e+07     c
8  Native_D707 6W    fungi   6W 8.64e+06     b
9    Notill_D0 0W bacteria   0W 2.98e+08     a
10 Notill_D707 0W bacteria   0W 7.79e+07     b
11   Native_D0 0W bacteria   0W 2.33e+08     c
12 Native_D707 0W bacteria   0W 2.20e+08     d
13   Notill_D0 6W bacteria   6W 3.37e+08     a
14 Notill_D707 6W bacteria   6W 8.84e+07     d
15   Native_D0 6W bacteria   6W 3.24e+08     c
16 Native_D707 6W bacteria   6W 1.89e+08     b

dput:

structure(list(trt = structure(c(5L, 7L, 1L, 3L, 6L, 8L, 2L, 
4L, 5L, 7L, 1L, 3L, 6L, 8L, 2L, 4L), .Label = c("Native_D0 0W", 
"Native_D0 6W", "Native_D707 0W", "Native_D707 6W", "Notill_D0 0W", 
"Notill_D0 6W", "Notill_D707 0W", "Notill_D707 6W"), class = "factor"), 
    org = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("bacteria", "fungi"), class = "factor"), 
    time = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0W", "6W"), class = "factor"), 
    copy = c(97800000, 15500000, 40200000, 10400000, 55100000, 
    14300000, 1.6e+07, 8640000, 2.98e+08, 77900000, 2.33e+08, 
    2.2e+08, 3.37e+08, 88400000, 3.24e+08, 1.89e+08), group = structure(c(1L, 
    2L, 3L, 4L, 1L, 4L, 3L, 2L, 1L, 2L, 3L, 4L, 1L, 4L, 3L, 2L
    ), .Label = c("a", "b", "c", "d"), class = "factor")), .Names = c("trt", 
"org", "time", "copy", "group"), class = "data.frame", row.names = c(NA, 
-16L))

代码:

    colvec <-c("blue", "orange")

ggplot(Suz2, aes(factor(trt), copy)) + 
  geom_bar(aes(fill = factor(org)), stat="identity", width = 0.7) +
  scale_fill_manual(values = colvec)+
  theme(panel.background = element_rect(fill='white', colour='white'), 
        panel.grid = element_line(color = NA),
        panel.grid.minor = element_line(color = NA),
        panel.border = element_rect(fill = NA, color = "black"),
        axis.text.x  = element_text(size=10, colour="black", face = "bold"),  
        axis.title.x = element_text(vjust=0.1, face = "bold"),
        axis.text.y = element_text(size=12, colour="black"),
        axis.title.y = element_text(vjust=0.2, size = 12, face = "bold"))

1 个答案:

答案 0 :(得分:3)

我认为你可以用facet做你想做的事情,如果你制作的情节是p,只需添加:

p + facet_wrap(~group,nrow = 1) 

得到: enter image description here

如果你有不同之处,请重新定义。