ggplot2批量生成图片并组合图片

当我们获得多因素的原始数据的时候,就会根据不同的因素来绘制多组一样的图片。一个一个画当然也行,不过很累,这里,我来使用ggplot2批量生成一个类型的图片,并将他们拼接在一起。

这是某次血常规的数据(经过了修改),将数据整理成下面这种形式:

分组情况如下:

library(rio)

library(ggplot2)

library(patchwork)

rawdata <- import(file = "exp.xlsx",sheet = 1,col_names = T,na = "0",col_type = "numeric")   #从excel读取数据,并将空单元格和单元格值为0的数字变为NA

group <- import(file = "exp.xlsx",sheet = 2,col_names = T,na = "0")

rawdata <- merge(group,rawdata,by = "order")    #按照order列合并,加入分组信息

rawdata

这里我们是想要每一列都做一个柱形图,因此可以写一个for循环,基本思路就是先把要做图的那一列变量和group列提取出来,然后对每一个变量计算各组的均值和标准差(均值就是柱子的高度,标准差是为了画出误差棒)。

需要先画一个图(本例就是先画出WBC,然后给出布局,例如这里要画14个图,我打算画四行,每行4个图,最后一行两个图):

data <- rawdata[,c(2,3)]

data <- na.omit(data)  #删除NA值

frame <- data.frame()  #使用循环计算出mean和sd

  for (j in c(1:7)){

    variable <- data[which(data$group == group[j,2]),]

    mean <- mean(variable[,2])

    sd <- sd(variable[,2])

    frame <- rbind(frame,cbind(mean = mean,sd = sd,order = j,group = group[j,2]))}

  frame$mean <- as.numeric(frame$mean)

  frame$sd <- as.numeric(frame$sd)

  p <- ggplot(frame,aes(x = reorder(group,order),y = mean))+

    geom_errorbar(aes(ymin = mean-sd ,ymax = mean + sd),size = 1.2,width = 0.2,color = "gray")+

    geom_bar(size = 1.2,color = "black",stat = "identity",width = 0.7,fill = rainbow(7),alpha = 0.6)+

    geom_point(shape = 18,size = 6,color = rainbow(7),alpha = 0.6)+

    labs(x = colnames(data)[2],y = paste("The organ coefficient of",colnames(data)[2]))+

    coord_cartesian(xlim = c(0.5,7.5),ylim = c(0,max(frame$mean+frame$sd)*1.2),expand = F)+

    geom_text(x = 4,y = max(frame$mean+frame$sd)*1.15,label = "One-way ANOVA: P > 0.05",size = 6)+

    theme(panel.background = element_blank(),

          panel.grid.major.y = element_line(colour = "grey",linetype = 2),

          axis.line = element_line(colour = "black",size = rel(2),arrow = arrow(angle = 30,length = unit(0.1,"inches"))),

          axis.title.y = element_text(size = rel(2),hjust = 0.5),

          axis.title.x = element_text(size = rel(2),hjust = 0.5),

          axis.text.x = element_text(size = rel(2),hjust = 1,angle = 45),

          axis.text.y = element_text(hjust = 1,size = rel(2)),

          axis.ticks = element_line(size = rel(1.3)),

          plot.title = element_text(size = rel(1.8)),

          plot.margin = margin(15,9,9,30))

  graph_one <- p + plot_layout(nrow = 4,ncol = 4,tag_level = "new")+plot_annotation(tag_levels = "A")+theme(plot.tag = element_text(size = rel(2)))         #这个函数就是给出整个图片的布局

这个图片就相当于是个画布

下面用for循环,将剩下的变量(WBC后面的变量)按照和上面一样的操作进行绘图,并将他们拼在一起。

for (i in c(4:dim(rawdata)[2])){

  data <- rawdata[,c(2,i)]

  data <- na.omit(data)

  frame <- data.frame()

  for (j in c(1:7)){

    variable <- data[which(data$group == group[j,2]),]

    mean <- mean(variable[,2])

    sd <- sd(variable[,2])

    frame <- rbind(frame,cbind(mean = mean,sd = sd,order = j,group = group[j,2]))}

    frame$mean <- as.numeric(frame$mean)

    frame$sd <- as.numeric(frame$sd)

    p <- ggplot(frame,aes(x = reorder(group,order),y = mean))+

      geom_errorbar(aes(ymin = mean-sd ,ymax = mean + sd),size = 1.2,width = 0.2,color = "gray")+

      geom_bar(size = 1.2,color = "black",stat = "identity",width = 0.7,fill = rainbow(7),alpha = 0.6)+

      geom_point(shape = 18,size = 6,color = rainbow(7),alpha = 0.6)+

      labs(x = colnames(data)[2],y = paste("The organ coefficient of",colnames(data)[2]))+

      coord_cartesian(xlim = c(0.5,7.5),ylim = c(0,max(frame$mean+frame$sd)*1.2),expand = F)+

      geom_text(x = 4,y = max(frame$mean+frame$sd)*1.15,label = "One-way ANOVA: P > 0.05",size = 6)+

      theme(panel.background = element_blank(),

            panel.grid.major.y = element_line(colour = "grey",linetype = 2),

            axis.line = element_line(colour = "black",size = rel(2),arrow = arrow(angle = 30,length = unit(0.1,"inches"))),

            axis.title.y = element_text(size = rel(2),hjust = 0.5),

            axis.title.x = element_text(size = rel(2),hjust = 0.5),

            axis.text.x = element_text(size = rel(2),hjust = 1,angle = 45),

            axis.text.y = element_text(hjust = 1,size = rel(2)),

            axis.ticks = element_line(size = rel(1.3)),

            plot.title = element_text(size = rel(1.8)),

            plot.margin = margin(15,9,9,30))

    graph_one <- graph_one + p + plot_layout(nrow = 4,ncol = 4,tag_level = "new")+plot_annotation(tag_levels = "A")+theme(plot.tag = element_text(size = rel(2)))  #和上面的写法有一点不一样

  }

ggsave(graph_one,filename = "graph_one.tiff",width = 30,height = 22,dpi = 300,compression = "lzw")  #将图片保存


最终图片:

你可能感兴趣的:(ggplot2批量生成图片并组合图片)