简体   繁体   English

RScript - ggplot() 和 ggsave() 循环不遍历列表

[英]RScript - ggplot() and ggsave() loop not iterating through list

I am trying to use a loop with ggplot() and ggsave() to create a set of plots from a list of separate data frames using the following code:我正在尝试使用带有 ggplot() 和 ggsave() 的循环来使用以下代码从单独的数据框列表中创建一组图:

temp2 = list(gsub("*.txt.out$", "", list.files(pattern="*.out")))
for (i in 1:length(temp2)) {
    Eg_cluster = temp2[[i]]
    df = data.frame(eval(parse(text = Eg_cluster)))
    myplot = ggplot(df, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
        geom_gene_arrow() +
        facet_wrap(~ molecule, scales = "free", ncol = 3) +
        scale_fill_brewer(palette = "Set3") +
        theme_genes() + ggtitle("snoRNA Cluster ", Eg_cluster)
    ggsave(file = print(paste0(Eg_cluster, ".pdf")), plot = myplot, device = "pdf")
}

The code successfully outputs the first plot from the first data frame in the list but the loop does not seem to iterate through the list and produce the rest of the plots in ggsave.该代码成功地从列表中的第一个数据帧输出了第一个 plot,但循环似乎没有遍历列表并在 ggsave 中生成图的 rest。 Does anyone have any insights as to why the rest of the plots are not outputted?有没有人对为什么不输出绘图的 rest 有任何见解?

Many thanks in advance!提前谢谢了!

You are making temp 2 a list of length 1.您正在将 temp 2 设为长度为 1 的列表。

temp2 = gsub("*.txt.out$", "", list.files(pattern="*.out"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM