多个grid.draw绘图,具有绘图功能

时间:2016-08-09 13:38:46

标签: r ggplot2 gridextra

我在this link上有数据框(文件名= Fish11a.rda)

load("Fish11a.rda")
df=Fish11a
df=data.frame(Time=as.factor(df[,6]),RoiID=df[,3],Diameter=df[,8])
df$Time.hours=rep(1:10,each=104*max(df$RoiID),len=nrow(df))
df$Time <- factor(df$Time, levels=rev(levels(df$Time)))
df1=split(df,df$Time.hours)

现在我有df1: df1$"1",df1$"2",.....,df1$"10"

的10个数据框

然后写了一个函数hle1来绘制来自该数据的图:

hle1=function(dfr,br,tenplot,tenfile) {
  require(ggplot2)
  require(gtable)
  library(grid)
  bre=lapply(split(df,df$Time.hours), function(x) {
    br=data.frame(x[c(1,round(nrow(x)*(1:4)/4)),])
    br$Min=c(0,15,30,45,60)
    return(br)
  })
  #start plot
  g=ggplot(data=dfr, aes(x=factor(RoiID), y=Time, fill = Diameter)) +
  theme_minimal()  + coord_fixed(ratio=1) +
  geom_tile(colour = NA, width = 1.5, height = 1)+ 
  scale_fill_gradient(low="black",high="white")+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(name="Time (min)",
                   expand = c(0,0),
                   breaks=br$Time,labels=br$Min)+
    theme(axis.ticks.length = unit(0, "lines"))+
  ggtitle(tenplot)
  
# calculate segment coordinates
segs <- c(.39, .23, .23, .15)
segs_center <- cumsum(segs) - segs/2
seg_ticks <- cumsum(segs)[1:3]
seg_labels <- paste("Seg", seq_along(segs))

# create graphicaal objects and gather as tree
grobs <- grobTree(linesGrob(c(0,1), c(-1,-1),gp=gpar(lwd=3)),
                  segmentsGrob(x0=seg_ticks, x1=seg_ticks, y0=0, y1=-4,gp=gpar(lwd=3)),
                  textGrob(x=segs_center, y=-3.5, 
                           label = seg_labels, hjust = .5, gp = gpar(cex =.9)))

# insert grobsTree in as annotation
g <- g + annotation_custom( grob = grobs,  
                            ymin = -.3, ymax = 0.2, 
                            xmin = .25, xmax = max(dfr$RoiID))

# override clipping for plotting outside of plotting area
gt <- ggplot_gtable(ggplot_build(g))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.newpage()
grid.draw(gt)}

我将使用函数hle1为上面的十个数据框绘制图(df1 $“1”,...,df1 $“10”)

然后,我想通过grid.arrange将10个图组合成一个图形并保存为tiff.file

tiff(file="test11.tiff", width = 17,height=30, units = 'cm', res = 300)
require(gridExtra)           
grid.arrange(hle1(df1$"1",bre$`1`,c("1 hour-feed")),
          hle1(df1$"2",bre$`2`,c("2 hours-feed")),
          hle1(df1$"3",bre$`3`,c("3 hours-feed")),
          hle1(df1$"4",bre$`4`,c("4 hours-feed")),
          hle1(df1$"5",bre$`5`,c("5 hours-feed")),
          hle1(df1$"6",bre$`6`,c("6 hours-feed")),
          hle1(df1$"7",bre$`7`,c("7 hours-feed")),
          hle1(df1$"8",bre$`8`,c("8 hours-feed")),
          hle1(df1$"9",bre$`9`,c("9 hours-feed")),
          hle1(df1$"10",bre$`10`,c("10 hours-feed")),
                          ncol=5,top="Fish 11 - feed")
dev.off()

但是我从df1 $“10”只获得了一个情节。 你们有谁知道为什么吗?在很长的问题上,我感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:1)

我在您的代码中看到了几个问题。 bre变量在hle1函数上传递,但似乎只在该函数内计算。另一个问题是,您不会返回hle1函数的结果,而是将其绘制在该函数内。如果您将grid.draw(gt)替换为gt,则代码可能有效。

这是我更新的代码,对我有用:

load("Fish11a.rda")
df=Fish11a
df=data.frame(Time=as.factor(df[,6]),RoiID=df[,3],Diameter=df[,8])
df$Time.hours=rep(1:10,each=104*max(df$RoiID),len=nrow(df))
df$Time <- factor(df$Time, levels=rev(levels(df$Time)))
df1=split(df,df$Time.hours)

hle1=function(dfr,tenplot) {
  require(ggplot2)
  require(gtable)
  library(grid)
  br=lapply(split(df,df$Time.hours), function(x) {
    br=data.frame(x[c(1,round(nrow(x)*(1:4)/4)),])
    br$Min=c(0,15,30,45,60)
    return(br)
  })
  #start plot
  g=ggplot(data=dfr, aes(x=factor(RoiID), y=Time, fill = Diameter)) +
    theme_minimal()  + coord_fixed(ratio=1) +
    geom_tile(colour = NA, width = 1.5, height = 1)+ 
    scale_fill_gradient(low="black",high="white")+
    theme(axis.title.x=element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank())+
    scale_x_discrete(expand = c(0,0))+
    scale_y_discrete(name="Time (min)",
                     expand = c(0,0),
                     breaks=br$Time,labels=br$Min)+
    theme(axis.ticks.length = unit(0, "lines"))+
    ggtitle(tenplot)

  # calculate segment coordinates
  segs <- c(.39, .23, .23, .15)
  segs_center <- cumsum(segs) - segs/2
  seg_ticks <- cumsum(segs)[1:3]
  seg_labels <- paste("Seg", seq_along(segs))

  # create graphicaal objects and gather as tree
  grobs <- grobTree(linesGrob(c(0,1), c(-1,-1),gp=gpar(lwd=3)),
                    segmentsGrob(x0=seg_ticks, x1=seg_ticks, y0=0, y1=-4,gp=gpar(lwd=3)),
                    textGrob(x=segs_center, y=-3.5, 
                             label = seg_labels, hjust = .5, gp = gpar(cex =.9)))

  # insert grobsTree in as annotation
  g <- g + annotation_custom( grob = grobs,  
                              ymin = -.3, ymax = 0.2, 
                              xmin = .25, xmax = max(dfr$RoiID))

  # override clipping for plotting outside of plotting area
  gt <- ggplot_gtable(ggplot_build(g))
  gt$layout$clip[gt$layout$name == "panel"] <- "off"
  grid.newpage()
  #grid.draw(gt)
  gt
}
#Run code snippetCopy snippet to answerExpand snippet
#I will use the function "hle1" to make plot for the ten data frames above (df1$"1",...,df1$"10") And then, I want to put 10 plots made into one figure by grid.arrange and save as a tiff.file

tiff(file="test11.tiff", width = 17,height=30, units = 'cm', res = 300)
require(gridExtra)   
grid.arrange(hle1(df1$"1",c("1 hour-feed")),
             hle1(df1$"2",c("2 hours-feed")),
             hle1(df1$"3",c("3 hours-feed")),
             hle1(df1$"4",c("4 hours-feed")),
             hle1(df1$"5",c("5 hours-feed")),
             hle1(df1$"6",c("6 hours-feed")),
             hle1(df1$"7",c("7 hours-feed")),
             hle1(df1$"8",c("8 hours-feed")),
             hle1(df1$"9",c("9 hours-feed")),
             hle1(df1$"10",c("10 hours-feed")),
             ncol=5,top="Fish 11 - feed")
dev.off()

#Run code snippetCopy snippet to answer

您可能还想查看构面网格,以便在ggplot中合并图形:http://docs.ggplot2.org/current/facet_grid.html