Claude Landry
Claude Landry

Reputation: 11

automating the saving of multiple graphs

I need to generate 170 graphs. When I try to automate the saving off all the graphs, only the first one appear with the right graphics the other ones are blank.

df <- read.csv("Full_data.csv", header = TRUE)
all.station <- as.vector(unique(df$Station))
for (i in 1:10) {
df.sub <- subset(df, df$Station == all.station[i])
df.1 <- df.sub[1:3, ]
v <- df.1[1,1]
z <- df.1[1, 3]
u <- df.sub[6, 3]

p1 = ggplot(...)
p2 = ggplot(...)
p3 = ggplot(...)
grid.arrange(p1, p2, p3, ncol = 3)
f.name <- paste(v, ".png", sep = "")
png(f.name)
ggsave(f.name, width=3.5, height=5, units = c("in"), dpi=100)

}
dev.off()

When I do it manually, all of them worked fine through the export command in the plot window. I tried using the method in cookbook but it only works for the first graph. Any suggestion about what I do wrong?

Upvotes: 0

Views: 32

Answers (1)

Claude Landry
Claude Landry

Reputation: 11

I was able thru research (matt to find out that ggsave don't work with grid.arrange.

so I had to use this that work perfectly:

    f.name <- paste(v, ".png", sep = "") +
    png(f.name) +
    grid.arrange(p1, p2, p3, ncol = 3)
    dev.off()
    }

Upvotes: 1

Related Questions