R ggplot2使用刻面网格具有不同x轴标签的刻面网格

R ggplot2使用刻面网格具有不同x轴标签的刻面网格,r,plot,ggplot2,geom-bar,facet-grid,R,Plot,Ggplot2,Geom Bar,Facet Grid,我在这个问题上纠缠了很长时间。基本上,我需要在二维平面网格中生成细菌分类群的堆叠条形图。这不是问题-但是,我确实需要使用2个不同的x轴标签 我已经研究了这些线程,它们很有帮助,但仍然不令人满意: 我的第一个数据帧: structure(list(variable = structure(c(1L, 1L, 1L, 15L, 15L, 15L, 8L, 8L, 8L, 14L, 14L, 14L, 11L, 11L, 11L, 6L, 6L, 6L, 9L, 9L, 9L, 2L, 2L

我在这个问题上纠缠了很长时间。基本上,我需要在二维平面网格中生成细菌分类群的堆叠条形图。这不是问题-但是,我确实需要使用2个不同的x轴标签

我已经研究了这些线程,它们很有帮助,但仍然不令人满意:

我的第一个数据帧:

structure(list(variable = structure(c(1L, 1L, 1L, 15L, 15L, 15L, 
8L, 8L, 8L, 14L, 14L, 14L, 11L, 11L, 11L, 6L, 6L, 6L, 9L, 9L, 
9L, 2L, 2L, 2L, 4L, 4L, 4L, 13L, 13L, 13L, 12L, 12L, 12L, 10L, 
10L, 10L, 3L, 3L, 3L, 7L, 7L, 7L, 5L, 5L, 5L), .Label = c("Control-5C10-7F", 
"Control-5C20-7M", "Treatment-5T16-7M", "Treatment-5T10-7F", 
"Treatment-5T20-7F", "Control-5C18-7F", "Treatment-5T17-7F", 
"Control-5C11-7F", "Control-5C19-7M", "Treatment-5T16-7F", "Control-5C17-7F", 
"Treatment-5T11-7M", "Treatment-5T10-7M", "Control-5C11-7M", 
"Control-5C10-7M"), class = "factor"), value = c(0.331838128419, 
0.352404886374, 0.0718854601356, 0.354207630618, 0.294475795902, 
0.0531792027053, 0.29236332445, 0.468273675353, 0.0607016856192, 
0.293872306838, 0.362196681338, 0.066892937885, 0.365593390631, 
0.307405564259, 0.0440421535284, 0.355757725414, 0.344565656834, 
0.0385728714381, 0.321804993859, 0.357477936005, 0.0189435852408, 
0.29485205349, 0.323279812948, 0.0613479971185, 0.330899082108, 
0.0950818222943, 0.00477515444166, 0.382399029908, 0.118943005246, 
0.00476898392234, 0.369726433283, 0.163932867784, 0.00531688661114, 
0.309218704951, 0.146836936202, 0.196198995646, 0.395112160831, 
0.185072972204, 0.00267743643869, 0.306483893184, 0.330640754636, 
0.0715509081212, 0.301275805191, 0.388917814857, 0.0356065705157
), Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("Control", "Treatment"), class = "factor"), 
    Time.Sex = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("7d.M", "7d.F"), class = "factor"), 
    IndividualID = c("5C10", "5C10", "5C10", "5C10", "5C10", 
    "5C10", "5C11", "5C11", "5C11", "5C11", "5C11", "5C11", "5C17", 
    "5C17", "5C17", "5C18", "5C18", "5C18", "5C19", "5C19", "5C19", 
    "5C20", "5C20", "5C20", "5T10", "5T10", "5T10", "5T10", "5T10", 
    "5T10", "5T11", "5T11", "5T11", "5T16", "5T16", "5T16", "5T16", 
    "5T16", "5T16", "5T17", "5T17", "5T17", "5T20", "5T20", "5T20"
    ), Sex = c("F", "F", "F", "M", "M", "M", "F", "F", "F", "M", 
    "M", "M", "F", "F", "F", "F", "F", "F", "M", "M", "M", "M", 
    "M", "M", "F", "F", "F", "M", "M", "M", "M", "M", "M", "F", 
    "F", "F", "M", "M", "M", "F", "F", "F", "F", "F", "F"), Rep = c(1, 
    1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 3, 3, 
    3, 4, 4, 4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 
    3, 3, 3, 4, 4, 4), IndividualID.Sex = c("5C10.F", "5C10.F", 
    "5C10.F", "5C10.M", "5C10.M", "5C10.M", "5C11.F", "5C11.F", 
    "5C11.F", "5C11.M", "5C11.M", "5C11.M", "5C17.F", "5C17.F", 
    "5C17.F", "5C18.F", "5C18.F", "5C18.F", "5C19.M", "5C19.M", 
    "5C19.M", "5C20.M", "5C20.M", "5C20.M", "5T10.F", "5T10.F", 
    "5T10.F", "5T10.M", "5T10.M", "5T10.M", "5T11.M", "5T11.M", 
    "5T11.M", "5T16.F", "5T16.F", "5T16.F", "5T16.M", "5T16.M", 
    "5T16.M", "5T17.F", "5T17.F", "5T17.F", "5T20.F", "5T20.F", 
    "5T20.F"), Genus = c("o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group", 
    "o__Bacteroidales;g__Parabacteroides", "o__Lactobacillales;g__Lactobacillus", 
    "o__Clostridiales;g__Lachnospiraceae NK4A136 group", "o__Bacteroidales;g__Parabacteroides", 
    "o__Lactobacillales;g__Lactobacillus", "o__Clostridiales;g__Lachnospiraceae NK4A136 group"
    ), variable2 = c("7d.F-1", "7d.F-1", "7d.F-1", "7d.M-1", 
    "7d.M-1", "7d.M-1", "7d.F-2", "7d.F-2", "7d.F-2", "7d.M-2", 
    "7d.M-2", "7d.M-2", "7d.F-3", "7d.F-3", "7d.F-3", "7d.F-4", 
    "7d.F-4", "7d.F-4", "7d.M-3", "7d.M-3", "7d.M-3", "7d.M-4", 
    "7d.M-4", "7d.M-4", "7d.F-1", "7d.F-1", "7d.F-1", "7d.M-1", 
    "7d.M-1", "7d.M-1", "7d.M-2", "7d.M-2", "7d.M-2", "7d.F-2", 
    "7d.F-2", "7d.F-2", "7d.M-3", "7d.M-3", "7d.M-3", "7d.F-3", 
    "7d.F-3", "7d.F-3", "7d.F-4", "7d.F-4", "7d.F-4"), pos = c(0.1, 
    0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 
    0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 
    0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
    0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)), .Names = c("variable", 
"value", "Type", "Time.Sex", "IndividualID", "Sex", "Rep", "IndividualID.Sex", 
"Genus", "variable2", "pos"), row.names = c(NA, -45L), class = "data.frame")
ggplot内部函数中使用的第二个数据帧

structure(list(variable2 = c("7d.F-1", "7d.M-1", "7d.F-2", "7d.M-2", 
"7d.F-3", "7d.F-4", "7d.M-3", "7d.M-4", "7d.F-1", "7d.M-1", "7d.M-2", 
"7d.F-2", "7d.M-3", "7d.F-3", "7d.F-4"), IndividualID = c("5C10", 
"5C10", "5C11", "5C11", "5C17", "5C18", "5C19", "5C20", "5T10", 
"5T10", "5T11", "5T16", "5T16", "5T17", "5T20"), Type = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Control", 
"Treatment"), class = "factor")), .Names = c("variable2", "IndividualID", 
"Type"), row.names = c(1L, 4L, 7L, 10L, 13L, 16L, 19L, 22L, 25L, 
28L, 31L, 34L, 37L, 40L, 43L), class = "data.frame")
我的代码:

index_x <<-  0
index_k <<-  0
p <- ggplot(data=df, aes(x=variable2, y=value, fill=Genus)) +
   facet_grid("Type ~ Time.Sex", scales="free_x", space="free_x") +
   geom_bar(stat="identity", size=0.0) +
   geom_bar( size=0.0, stat="identity", show.legend=FALSE) +
##I tried with some level of success to include the labels I want inside the stacked bars themselves, but 
   ## would ideally like them to be on the x-axis.
   geom_text(aes(y=pos, label=IndividualID), vjust=0, angle=90, size=3) +
   xlab("Category") +
   ylab("Abundance %") +
   ggtitle(paste0("Taxa abundance")) +
   theme(
      panel.border=element_rect(fill=NA, linetype="solid", colour = "black", size=0.25),
      axis.text.x=element_text(size=9, colour="black", angle=as.numeric(90)),
      axis.text.y=element_text(size=9, colour="black"),
      axis.title=element_text(family="Helvetica", size=10),
      plot.title = element_text(lineheight=1.2, face="bold", size=25),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      panel.background=element_blank(),
      legend.key.size = unit(0.35, "cm"),
      legend.text = element_text(size=12, face="bold"),
      legend.title = element_text(size=12, face="bold"),
      legend.spacing = unit(1, "cm"),
      legend.position="right",
      strip.text.x = element_text(angle=90, vjust=0, size=7, face="bold"),
      strip.text.y = element_text(hjust=0, angle=0, size=7, face="bold"),
      strip.background =  element_blank(),
      panel.spacing.y = unit(0.3, "lines")
   ) +  scale_fill_manual(values=vColors) +
   ## I wrote an internal function to force the x labels
   scale_x_discrete(name="IndividualID", labels = function(x){

      final_labels = c()

      if(index_x %% length(curr_order_x) == 0){
         index_k <<- index_k + 1
      }
      df_labels2 = df_labels_orig[df_labels_orig[[ facets[1] ]] == rev(curr_order_y)[index_k], ]

      for(i in 1:length(x)){
         final_labels = c(final_labels, df_labels2[df_labels2$variable2 == x[i],]$IndividualID)
      }
      index_x <<- index_x + 1

      print(final_labels)
      return(final_labels)
   }  
   )
print(p)

<代码>索引xx,而不是调整<代码> FACETYGRADEG/<代码>的设置,考虑根据需要的分组单独构建图。具体来说,考虑基础R的<>代码> (通过因子(s)来分割数据文件)以根据您的级别、类型和时间构建一个图的列表。 但是,需要进行调整以满足您的要求:

  • 将对照区置于处理区上方
  • 调整轴以避免冗余(根据图的实际数量定制,此处为4):
    • 删除第2和第4页上的y轴标题/文本/记号标记
    • 删除第一个和第二个绘图上的x轴标题
  • 使用方法避免多个图例
  • 共享图例(调整为在绘图列表中作为第一个参数读取,并在顶部添加标题)

    #学分:@浸信会:https://github.com/tidyverse/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
    
    网格(排列)(共享)(图例)您是否愿意使用不同的方法来处理此图(即非
    facet(网格)?是-使用ggplot2?
    # Then following the references I found on SO, I tried the following:
    index_x <<- 0 
    index_k <<- 0
    
    g <- ggplotGrob(p)
    
    panels <- grep("panel", g$layout$name)
    top <- unique(g$layout$t[panels])
    
    top.row <- gtable:::rbind_gtable(
       g[seq.int(min(top)), ],
       g[max(top)+1,], "first"
    )
    bottom.row <- g[(max(top)-1):nrow(g), ]
    all <- gtable:::rbind_gtable(top.row, bottom.row, "first")
    grid.newpage()
    grid.draw(all)
    
    # CREDIT: @Baptist: https://github.com/tidyverse/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
    grid_arrange_shared_legend <- function(plots, ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
    
      position <- match.arg(position)
      g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
      legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
      lheight <- sum(legend$height)
      lwidth <- sum(legend$width)
      gl <- lapply(plots, function(x) x + theme(legend.position="none"))
      gl <- c(gl, ncol = ncol, nrow = nrow)
    
      title <- textGrob("Taxa Abundance",gp=gpar(fontsize=20))
    
      combined <- switch(position,
                         "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                                legend,
                                                ncol = 1,
                                                top = title,
                                                heights = unit.c(unit(0.95, "npc") - lheight, lheight)),
                         "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                               legend,
                                               ncol = 2,
                                               widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
    
      grid.newpage()
      grid.draw(combined)
    
      # return gtable invisibly
      invisible(combined)
    
    }
    
    vColors <- c("red", "green", "blue")
    
    by_plots <- by(df, df[,c("Type", "Time.Sex")], FUN=function(sub)       
      ggplot(sub, aes(x=IndividualID, y=value, fill=factor(Genus))) +
        geom_bar(stat="identity", size=0.0) + 
        labs(title=paste(sub$Type[[1]], "-", sub$Time.Sex[[1]]), x="Category", y="Abundance %") + 
        guides(fill=guide_legend(title="Genus", nrow=3)) + ylim(0, 0.9) +
        scale_fill_manual(values=vColors) +
        theme(plot.title = element_text(hjust = 0.5))
    )
    
    # RE-ORDER FOR CONTROL PLOTS ABOVE TREATMENT PLOTS
    by_plots <- by_plots[c(1,3,2,4)]
    
    # CONDITIONALLY REMOVE Y-AXIS AND X-AXIS TITLE    
    by_plots <- lapply(seq_along(by_plots), function(i) {
      if ((i %% 2) == 0)
        by_plots[[i]] <- by_plots[[i]] + theme(axis.title.y = element_blank(),
                                               axis.text.y = element_blank(),
                                               axis.ticks.y = element_blank())
    
      if (i < 3)
        by_plots[[i]] <- by_plots[[i]] + theme(axis.title.x = element_blank())      
    
      return(by_plots[[i]])
    })
    
    grid_arrange_shared_legend(by_plots, ncol=2, nrow=2)