R批量绘制误差折线图

2021-01-18  本文已影响0人  蜡笔小生信

先上图

image.png
数据形式
1、表格
image.png
2、分组(由于R识别不了“—”,自动换为".")
image.png
*****代码*****
library(reshape2)
library(Rmisc)
library(ggplot2)
library(reshape2)
qinya <- read.csv("xiao.CSV",header = T)
group <- read.csv("group.CSV",header = T)
##############################
chouqinya <- function(data= data1,group = group,num = 1, aa="a1",bb="b1", cc="c1",dd="d1",ee="e1" ,ff="f1" , x1=1,x2=1,x3=1,x4=1,x5=1,x6=1,x7= 1){
  list1 <- list()
  namespaste <- c()
  names <- c(aa,bb,cc,dd,ee,ff)
  date <- c(x1,x2,x3,x4,x5,x6,x7)
  b = 0
  a = 0
  c = 0
  for (i in 1:num) {
    namespaste[i] <- paste0(names[i],".csv")
    if (i == 1) {
      list1[[i]] <- data[1:date[i],]
    }
    if(i >1 ){
      a = date[i-1]
      b = a + b
      c = b+ date[i]
      list1[[i]] <- data[(b+1):c,]
    }
  }
  liste <- list()
  picture <- c()
  for (i in 1:length(list1)) {
    row.names(list1[[i]]) <- list1[[i]]$date
    eg <- list1[[i]][,-1]
    eg <- as.data.frame(t(eg))
    eg$sample <- factor(rownames(eg), levels = rev(rownames(eg)))
    eg <- melt(eg, id = 'sample')
    eg <- merge(eg,group,by="sample")
    eg$value <- as.numeric(eg$value)
    liste[[i]] <- summarySE(eg, measurevar="value", groupvars=c("group","variable"))
    write.csv(liste[[i]],file=namespaste[i],row.names = F)    
    plota <- liste[[i]]
    p1 <- ggplot(plota, aes(x=factor(variable), y=value, colour=group,group=group,shape=group,fill=group)) + 
        geom_line() +geom_point(size=4)+geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1)+
        theme(panel.grid.major =element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),axis.line = element_line(colour = "black"))
    ggsave(paste0(names[i],".pdf"),p1,device = "pdf",width = 13,height = 9,dpi = 400) 
  }
}

chouqinya(data = qinya, group = group ,num = 5,aa ="jd" ,bb ="cod" ,cc ="ad" ,dd ="ph" ,ee = "orp",
          x1 =3 ,x2 =3 ,x3 =3 ,x4 =22 ,x5 = 23)
#data = qinya 代表数据框
#num =1  代表数据种类
#aa,bb等代表数据名字
#x1,x2等代表每种数据的行数
上一篇下一篇

猜你喜欢

热点阅读