r - 创建一个循环以生成从三个单独的 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)
解决方案
如果您有一个包含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)
}
推荐阅读
- java - 调用 XMLSignature.validate 并在 JBOSS 7 上运行时获取“当前节点:[#document:null],类型:9”
- reporting-services - CRM 报告扩展 - SSRS 实例为空白
- android - 更新 NDK 构建后未生成
- angular - Angular 6.0 和 Karma Jasmine - 浏览器在测试完成后关闭
- azure - Azure 人脸 API 对同一个人的图像的信心减弱
- c++ - 如何修改链表中的用户输入?
- orientdb - 在 OrientDB 中使用 SQL 查找两个顶点之间排除的替代路径
- node.js - 在快速路由响应中发送 PDF 并强制浏览器下载
- tensorflow - 如何检查 tf.data 数据集对象?
- mysql - MySql-Maven-Plugin:java.sql.SQLException:用户'root'@'localhost'的访问被拒绝(使用密码:是)