首页 > 解决方案 > 创建一个循环以生成从三个单独的 CSV 文件读取的三个单独的图

问题描述

我有三个基本上格式相同的 csv 文件。我为其中一个文件生成了一个堆积条形图,我想知道是否有办法生成一个循环来读取其他两个文件并生成我的其他图。

这是我的代码:

DE_iterative_genotype_nr_12_sign_only <- read_delim("DE_iterative_genotype_nr_12_sign_only.csv", 
                                                    ";", escape_double = FALSE, trim_ws = TRUE) %>%
  melt()

TM_df_up <- DE_iterative_genotype_nr_12_sign_only %>% 
  filter(value>0) %>% #filtering for upregulated genes
  group_by(variable) %>% 
  count() %>% #summarises to give the number of DEGs per comparison
  as.data.frame() %>% 
  mutate(direction="up")

TM_df_down <- DE_iterative_genotype_nr_12_sign_only %>% 
  filter(value<0) %>% #filtering for downregulated genes
  group_by(variable) %>% 
  count() %>% 
  as.data.frame() %>% 
  mutate(n=n*-1,direction="down") 

TM_df_plot <- rbind(TM_df_up, TM_df_down)

ggplot(TM_df_plot, (aes(x=variable, y=n, 
                        fill=direction, #up or down regulated
                        label=abs(n))))+ #label of number of DEGs as an absolute value
  geom_bar(stat="identity", aes(alpha=0.1))+
  geom_text(size = 6)+
  labs(y=NULL,x=NULL)+
  scale_x_discrete(labels= c("18 vs 20", #adding manual labels 
                             "20 vs 22",
                             "22 vs 24",
                             "24 vs DS"))+
  labs(
    x = "Days after pollentation",
    title = "TM_abi3-12 dog1-4cyp707a2"
  )+
  scale_fill_manual(values = c("blue", "red"))+ #changing the fill colours
  scale_y_continuous(breaks=seq(-2900, 16500, 550))+ #changing the scale breaks
  theme_minimal()+
  theme(
    plot.title = element_text(hjust = 0.5),
    text = element_text(size=24),
    panel.grid = element_blank(),
    axis.text.x = element_text(angle=90, vjust=0.35, hjust = 3),
    legend.position = "none"
  )

ggsave("triple mutant significant.png", width=10, height=27, dpi=300) 

标签: rloopsggplot2

解决方案


如果您有一个包含myfiles文件路径的字符向量和一个myplots包含要保存的图的路径的字符向量,则可以循环遍历:

myfiles <- c('file1.csv', 'file2.csv', 'file3.csv')
myplots <- c('plot1.png', 'plot2.png', 'plot3.png')
for(i in 1:length(myfiles)) {   
  data <- read_delim(myfiles[i], 
                     ";", 
                     escape_double = FALSE, 
                     trim_ws = TRUE) %>% melt()

  TM_df_up <- data %>% 
    filter(value>0) %>% #filtering for upregulated genes
    group_by(variable) %>% 
    count() %>% #summarises to give the number of DEGs per comparison
    as.data.frame() %>% 
    mutate(direction="up")

  TM_df_down <- data %>% 
    filter(value<0) %>% #filtering for downregulated genes
    group_by(variable) %>% 
    count() %>% 
    as.data.frame() %>% 
    mutate(n=n*-1,direction="down") 

  TM_df_plot <- rbind(TM_df_up, TM_df_down)

  ggplot(TM_df_plot, (aes(x=variable, y=n, 
                          fill=direction, #up or down regulated
                          label=abs(n))))+ #label of number of DEGs as an absolute value
    geom_bar(stat="identity", aes(alpha=0.1))+
    geom_text(size = 6)+
    labs(y=NULL,x=NULL)+
    scale_x_discrete(labels= c("18 vs 20", #adding manual labels 
                               "20 vs 22",
                               "22 vs 24",
                               "24 vs DS"))+
    labs(
      x = "Days after pollentation",
      title = "TM_abi3-12 dog1-4cyp707a2"
    )+
    scale_fill_manual(values = c("blue", "red"))+ #changing the fill colours
    scale_y_continuous(breaks=seq(-2900, 16500, 550))+ #changing the scale breaks
    theme_minimal()+
    theme(
      plot.title = element_text(hjust = 0.5),
      text = element_text(size=24),
      panel.grid = element_blank(),
      axis.text.x = element_text(angle=90, vjust=0.35, hjust = 3),
      legend.position = "none"
    )
  ggsave(myplots[i], width=10, height=27, dpi=300) 
}

推荐阅读