1

I want to save multiple plots as png using a loop. Unfortunately the loop doesn't seem to work, as the saving and plotting of one single plot works but if I do it within the loop, nothing is saved. I don't receive a error message either. Just nothing happens.

Here is the code that works:

name = main_emo[i]
  mypath=file.path("/Users/Jasmin/Documents/Psychologie/Master/Masterarbeit/Working directory", name)

  png(mypath)

  qplot(x_emo,y_emo, main = main_emo[i]) + geom_errorbar(aes(x=x_emo, ymin=y_emo-sd, ymax=y_emo+sd), width=0.25) + ylab("Rating") + xlab("Emotion")+
    theme(panel.grid.major = element_blank()) +  scale_y_continuous(breaks=seq(1,7,1)) + expand_limits(y=7)

  dev.off()

and this is the loop where it doesn't work anymore:

main_emo <- c("Emotion Profile Funeral", "Emotion Profile Wedding", "Emotion Profile Destroyed Street","Emotion Profile Destroyed Street")  

frame_name <- c("nice", "wedd", "des", "fun")
emo_mean <- c("rmean_EM_Trauer_", "rmean_EM_Freude_","rmean_EM_Angst_", "rmean_EM_Aerger_", "rmean_EM_Ekel_", "rmean_EM_Ueber_")


for (i in 1: length(frame_name)) {
  y_emo <- c()
  sd <- c()
  x_emo <- c("Trauer", "Freude", "Angst", "Aerger", "Ekel", "Üeberraschung")

  for (j in 1: length(emo_mean)) {
    y_col <- unlist(pre_sub[colnames(pre_sub) == paste0(emo_mean[j], frame_name[i])], use.names=FALSE)
    y_emo <- c(y_emo, mean(y_col, na.rm=TRUE))
    sd <- c(sd, sd(y_col, na.rm=TRUE))
  }

  name = main_emo[i]
  mypath=file.path("/Users/Jasmin/Documents/Psychologie/Master/Masterarbeit/Working directory", name)

  png(mypath)

  qplot(x_emo,y_emo, main = main_emo[i]) + geom_errorbar(aes(x=x_emo, ymin=y_emo-sd, ymax=y_emo+sd), width=0.25) + ylab("Rating") + xlab("Emotion")+
    theme(panel.grid.major = element_blank()) +  scale_y_continuous(breaks=seq(1,7,1)) + expand_limits(y=7)

  dev.off()
}

Thanks for your help!

2
  • Check if mypath contains correct character vector specifying the path and the name. The space in working directory might be causing problems. Commented Apr 15, 2019 at 10:28
  • You're using qplot() from ggplot2 so you might have more luck with ggsave(). Commented Apr 15, 2019 at 10:57

1 Answer 1

1

Use ggsave()

Doesn't work:

library("ggplot2")
for (i in unique(diamonds$color)) {
  png(paste0("~/Desktop/colour_", i, ".png"))
  ggplot(diamonds, aes(carat, price)) + geom_point()
  dev.off()
}

Does work:

for (i in unique(diamonds$color)) {
  ggplot(diamonds, aes(carat, price)) + geom_point()
  ggsave(paste0("~/Desktop/color_", i, ".png"))
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it worked. If this solved your issue please tick to accept the answer so others can see this is resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.