R语言for循环绘图

12 篇文章 2 订阅
7 篇文章 0 订阅
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()
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值