• R语言for循环绘图


    a <- c('A_1h','A_12h','A_24h','A_48h',
           'N_1h','N_12h','N_24h','N_48h',
           'P_1h','P_12h','P_24h','P_48h')
    group <- rep(c('A','P','N'),each = 4)
    compound <- c('Ala','Arg','Asn','Asp','His',
                  '4-Hydroxybenzoic acid','3-Phenylpropionic acid',
                  'trans-Cinnamic acid','Phthalic acid',
                  'Quercetin','Taxifolin','Luteolin',
                  'NARINGENIN','trans-Chalcone','Isorhamnetin',
                  'SPM','SPD','PUT')
    library(ggplot2)
    library(ggprism)
    library(RColorBrewer)
    
    df <- data.frame('sample' = a,
                     'group' = group,
                     'mean' = runif(12, 1.0, 80.5),
                     'coefficient' = runif(12, 0.01, 0.1))
    df$sd <- df$mean*df$coefficient
    colnames(df)
    df$sample = factor(df$sample,levels=a)
    ggplot(df,aes(x = sample, y = mean, fill = group, col = group))+
      geom_bar(stat = 'identity',width = .6)+
      ggprism::theme_prism()+
      theme(axis.text.x = element_text(angle = 90))+
      labs(x= '',y = '',title = compound[1])+
      scale_fill_manual(values = brewer.pal(3,'Dark2'))+
      scale_color_manual(values = brewer.pal(3,'Dark2'))+
      geom_errorbar(#data = df5,
                    aes(ymin=mean-sd, ymax=mean+sd), 
                    width=.2, 
                    position=position_dodge(0),
                    show.legend = FALSE,
                    col = 'black',size = .8)
    for (i in 1:18) {
      a <- c('A_1h','A_12h','A_24h','A_48h',
             'N_1h','N_12h','N_24h','N_48h',
             'P_1h','P_12h','P_24h','P_48h')
      group <- rep(c('A','P','N'),each = 4)
      compound <- c('Ala','Arg','Asn','Asp','His',
                    '4-Hydroxybenzoic acid','3-Phenylpropionic acid',
                    'trans-Cinnamic acid','Phthalic acid',
                    'Quercetin','Taxifolin','Luteolin',
                    'NARINGENIN','trans-Chalcone','Isorhamnetin',
                    'SPM','SPD','PUT')
      library(ggplot2)
      library(ggprism)
      library(RColorBrewer)
      
      df <- data.frame('sample' = a,
                       'group' = group,
                       'mean' = runif(12, 1.0, 80.5),
                       'coefficient' = runif(12, 0.01, 0.1))
      df$sd <- df$mean*df$coefficient
      colnames(df)
      df$sample = factor(df$sample,levels=a)
      jpeg(filename = paste0(compound[i],".jpg"),
           width = 900, height = 750, units = "px",
           pointsize = 20)
      print(ggplot(df,aes(x = sample, y = mean, fill = group, col = group))+
        geom_bar(stat = 'identity',width = .6)+
        ggprism::theme_prism()+
        theme(text = element_text(size = 24),
          axis.text.x = element_text(angle = 90))+
        labs(x= '',y = '',title = compound[i])+
        scale_fill_manual(values = brewer.pal(3,'Dark2'))+
        scale_color_manual(values = brewer.pal(3,'Dark2'))+
        geom_errorbar(#data = df5,
          aes(ymin=mean-sd, ymax=mean+sd), 
          width=.2, 
          position=position_dodge(0),
          show.legend = FALSE,
          col = 'black',size = .8))
      dev.off()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    Linux:无锁化编程 __sync_fetch_and_add原理及其实现分析
    交换机配置参考案例
    微信小程序怎么隐藏顶部导航栏(navigationBar)变透明的解决方案
    嵌入式笔试面试刷题(day5 IIC详解)
    《动手学深度学习 Pytorch版》 5.6 GPU
    react数据管理之setState与Props
    Spring 面向切面编程 第3关:AOP实现原理-JDK动态代理
    地图导航测试用例,你get了吗?
    【夜读】坚持这5个习惯,遇见更优秀的自己
    自己写并发工具是一种什么体验?
  • 原文地址:https://blog.csdn.net/weixin_47634487/article/details/128035627