简体   繁体   中英

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:

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. Does anyone have any insights as to why the rest of the plots are not outputted?

Many thanks in advance!

You are making temp 2 a list of length 1.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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