本文介绍了编写一个循环来创建具有不同数据源和标题的ggplot数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用循环的经验,但看起来我需要创建其中的一些来正确分析我的数据。你能否展示如何在我已经创建的代码上创建一个简单的循环?让我们使用循环来获得一些图:

  pdf(file = sprintf(complex I analysis,tbl_comp_abu1),paper =' A4r')

ggplot(df_tbl_data1_comp1,aes(Size_Range,Abundance,group = factor(Gene_Name)))+
theme(legend.title = element_blank())+
geom_line (aes(color = factor(Gene_Name)))+
ggtitle(Data1 - complex I)+
theme(axis.text.x = element_text(angle = 90,hjust = 1))

ggplot(df_tbl_data2_comp1,aes(Size_Range,Abundance,group = factor(Gene_Name)))+
theme(legend.title = element_blank())+
geom_line(aes(color = factor(Gene_Name)))+
ggtitle(Data2 - complex I)+
theme(axis.text.x = element_text(angle = 90,hjust = 1))


ggplot(df_tbl_data3_comp1,aes(Size_Range,Abundance,group = factor(Gene_Name)))+
theme(legend.title = element_blank())+
geom_line(aes(color = factor(Gene_Name)))+
ggtitle(Datas3 - complex I)+
theme(axis.text.x = element_text(angle = 90,h只是= 1))

dev.off()

现在的问题是我想达到的。所以首先我要分析10个复合体,这样就意味着需要创建10个pdf文件,这个例子显示了复杂体的三个不同数据集的图。要正确地将变量 comp1 (来自 df_tbl_dataX_comp1 )中的数字从1更改为10 - 取决于我们想要绘制的复杂。下一个必须通过循环改变的是pdf文件的名称和每个图表......是否可以编写这样的循环?



数据:

  structure(list(Size_Range = structure(c(1L,1L,1L,2L,2L,2L,
3L ,3L,3L,4L,4L,4L,5L,5L,5L,6L,6L,6L,7L,7L,7L,8L,
8L,8L,9L,9L,9L,10L,10L,10L ,11L,11L,11L,12L,12L,12L,
13L,13L,13L,14L,14L,14L,15L,15L,15L,16L,16L,16L,17L,
17L, ,18L,18L,18L,19L,19L,19L,20L,20L,20L),标签= c(10,
34,59,84,110 134,165,199,234,257,362,
433,506,581,652,733,818 ,896,972,1039
),class =factor),丰度= c(0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,142733.475,108263.525,98261.11,649286.165,
3320759.803,3708515.148,6691260.945,30946562.92,180974.3725,
4530005.805,21499827.89,0,15032198.54 ,4058060.583,0,3842964.97,
2544030.857,0.1 640476.977,286249.1775,0217388.5675,1252965.433,
0,1314666.05,167467.8825,0,253798.15,107244.9925,0207341.1925,
15755.485, 0,0,
0,0),Gene_Name = c(AT1G01080,AT1G01090,AT1G01320,AT1G01420,
AT1G01470,AT1G01560,AT1G01800 AT1G02530,AT1G02780,AT1G02880,AT1G02920,AT1G02930,
AT1G03030,AT1G03090,AT1G03110,AT1G03130 ,AT1G03220,
,AT1G03230,AT1G03330,AT1G03475,AT1G03630,AT1G03680,
AT1G03870ATCG00420ATCG00470ATCG00480 ATCG00690,ATCG00670,ATCG00740,
ATCG00750,ATCG00842,ATCG01100,ATCG01030,ATCG01114 ,
ATCG01665,ATCG00770,ATCG00780,ATCG00800,ATCG00810,
ATCG00820,ATCG00722,ATCG00744,ATCG00855,ATCG00853 b $ bATCG00888,ATCG00733,ATCG00766,ATCG00812,ATCG00821,
ATCG 00856,ATCG00830,ATCG00900,ATCG01060,ATCG01110,
ATCG01120)),.Names = c(Size_Range,Abundance,Gene_Name
) ,row.names = c(NA,-60L),class =data.frame)


解决方案

这可能会有诀窍:
启动两个循环,一个用于复杂迭代,另一个用于数据集迭代。然后使用 paste0() paste()生成正确的文件名和标题。



PS .:我没有测试代码,因为我没有你的数据。但它应该给你一个想法。



$ p $复合
(c在1:10){

#为每个复杂的
pdf创建pdf(file = paste0(complex,c,analysis.pdf),paper ='A4r')

#loop over数据集
for(d in 1:3){

#plot
ggplot(get(paste0(df_tbl_data,d,_ comp,c)) ,aes(Size_Range,Abundance,group = factor(Gene_Name)))+
theme(legend.title = element_blank())+
geom_line(aes(color = factor(Gene_Name)))+
ggtitle(paste0(Data,d, - complex,c))+
theme(axis.text.x = element_text(angle = 90,hjust = 1))
}
dev.off()

}


I do not have experience with loops but it looks like I will need to create some of them to analyze my data properly. Could you show how to create a simple loop on the code which I already created ? Let's use looping to get some plots:

pdf(file = sprintf("complex I analysis", tbl_comp_abu1), paper='A4r')

ggplot(df_tbl_data1_comp1, aes(Size_Range, Abundance, group=factor(Gene_Name))) +
  theme(legend.title=element_blank()) +
  geom_line(aes(color=factor(Gene_Name))) +
  ggtitle("Data1 - complex I")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggplot(df_tbl_data2_comp1, aes(Size_Range, Abundance, group=factor(Gene_Name))) +
  theme(legend.title=element_blank()) +
  geom_line(aes(color=factor(Gene_Name))) +
  ggtitle("Data2 - complex I")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))


ggplot(df_tbl_data3_comp1, aes(Size_Range, Abundance, group=factor(Gene_Name))) +
  theme(legend.title=element_blank()) +
  geom_line(aes(color=factor(Gene_Name))) +
  ggtitle("Datas3 - complex I")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

dev.off()

The question now is what I would like to achieve. So first of all I have like 10 complexes to analyze so that means 10 pdf files should be created and the example shows plots from three different data sets for the complex one. To make it properly the number in variable comp1 (from df_tbl_dataX_comp1) has to be changed from 1 to 10 - depends which complex we want to plot. The next thing which has to be changed through the loop is the name of pdf file and each of graphs... Is it possible to write such loop ?

Data:

structure(list(Size_Range = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 
8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
17L, 17L, 18L, 18L, 18L, 19L, 19L, 19L, 20L, 20L, 20L), .Label = c("10", 
"34", "59", "84", "110", "134", "165", "199", "234", "257", "362", 
"433", "506", "581", "652", "733", "818", "896", "972", "1039"
), class = "factor"), Abundance = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 142733.475, 108263.525, 98261.11, 649286.165, 
3320759.803, 3708515.148, 6691260.945, 30946562.92, 180974.3725, 
4530005.805, 21499827.89, 0, 15032198.54, 4058060.583, 0, 3842964.97, 
2544030.857, 0, 1640476.977, 286249.1775, 0, 217388.5675, 1252965.433, 
0, 1314666.05, 167467.8825, 0, 253798.15, 107244.9925, 0, 207341.1925, 
15755.485, 0, 71015.85, 14828.5075, 0, 25966.2325, 0, 0, 0, 0, 
0, 0), Gene_Name = c("AT1G01080", "AT1G01090", "AT1G01320", "AT1G01420", 
"AT1G01470", "AT1G01560", "AT1G01800", "AT1G02150", "AT1G02500", 
"AT1G02560", "AT1G02780", "AT1G02880", "AT1G02920", "AT1G02930", 
"AT1G03030", "AT1G03090", "AT1G03110", "AT1G03130", "AT1G03220", 
"AT1G03230", "AT1G03330", "AT1G03475", "AT1G03630", "AT1G03680", 
"AT1G03870", "ATCG00420", "ATCG00470", "ATCG00480", "ATCG00490", 
"ATCG00500", "ATCG00650", "ATCG00660", "ATCG00670", "ATCG00740", 
"ATCG00750", "ATCG00842", "ATCG01100", "ATCG01030", "ATCG01114", 
"ATCG01665", "ATCG00770", "ATCG00780", "ATCG00800", "ATCG00810", 
"ATCG00820", "ATCG00722", "ATCG00744", "ATCG00855", "ATCG00853", 
"ATCG00888", "ATCG00733", "ATCG00766", "ATCG00812", "ATCG00821", 
"ATCG00856", "ATCG00830", "ATCG00900", "ATCG01060", "ATCG01110", 
"ATCG01120")), .Names = c("Size_Range", "Abundance", "Gene_Name"
), row.names = c(NA, -60L), class = "data.frame")
解决方案

This might do the trick:Initiate two loops, one for the complex iteration and a second for the dataset iteration. Then use paste0() or paste() to generate the correct filenames and headings.

PS.: I didn't test the code, since I dont have your data. But it should give you an idea.

#loop over complex    
for (c in 1:10) {

    #create pdf for every complex 
    pdf(file = paste0("complex", c, "analysis.pdf"), paper='A4r')

    #loop over datasets
    for(d in 1:3) {

    #plot
    ggplot(get(paste0("df_tbl_data",d,"_comp",c)), aes(Size_Range, Abundance, group=factor(Gene_Name))) +
      theme(legend.title=element_blank()) +
      geom_line(aes(color=factor(Gene_Name))) +
      ggtitle(paste0("Data",d," - complex ",c))+
      theme(axis.text.x = element_text(angle = 90, hjust = 1))
    }   
    dev.off()

}

这篇关于编写一个循环来创建具有不同数据源和标题的ggplot数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:55