Hello R team,I want to use for loop to generate multiple plots with 3 parameter, (y is for y axis, c is for color and f is for file name in output). I created a data frame to save the information and use the information in for loop. I use y[i], c[i] and f[i] in the loop, but it seems doesn't work. Can anyone correct my code to make it work? Thanks,Kai library(ggplot2)library(tidyverse) y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in nrow(mac)){? mpg %>%? ? filter(hwy <35) %>%?? ? ggplot(aes(x = displ, y = y[i], color = c[i])) +?? ? geom_point()? ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")} [[alternative HTML version deleted]]
nrow() is just the numbers of rows in your data frame, use seq_len(nrow()) or seq(nrow()) to loop through all row numbers On Wed, Dec 22, 2021, 12:08 Kai Yang via R-help <r-help at r-project.org> wrote:> Hello R team,I want to use for loop to generate multiple plots with 3 > parameter, (y is for y axis, c is for color and f is for file name in > output). I created a data frame to save the information and use the > information in for loop. I use y[i], c[i] and f[i] in the loop, but it > seems doesn't work. Can anyone correct my code to make it work? > Thanks,Kai > > library(ggplot2)library(tidyverse) > y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class") > mac <- data.frame(y,c,f) > for (i in nrow(mac)){ mpg %>% filter(hwy <35) %>% ggplot(aes(x > displ, y = y[i], color = c[i])) + geom_point() > ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")} > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Try replacing "c:/temp/f[i].jpg" with paste0("c:/temp/",f[i],".jpg") On Wed, Dec 22, 2021 at 7:08 PM Kai Yang via R-help <r-help at r-project.org> wrote:> Hello R team,I want to use for loop to generate multiple plots with 3 > parameter, (y is for y axis, c is for color and f is for file name in > output). I created a data frame to save the information and use the > information in for loop. I use y[i], c[i] and f[i] in the loop, but it > seems doesn't work. Can anyone correct my code to make it work? > Thanks,Kai > > library(ggplot2)library(tidyverse) > y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class") > mac <- data.frame(y,c,f) > for (i in nrow(mac)){ mpg %>% filter(hwy <35) %>% ggplot(aes(x > displ, y = y[i], color = c[i])) + geom_point() > ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")} > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
You may have to add an explicit 'print' to ggplot library(ggplot2) library(tidyverse) y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in nrow(mac)){ mpg %>% filter(hwy <35) %>% print(ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point()) ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in") } Thanks Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. Thanks Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Wed, Dec 22, 2021 at 9:08 AM Kai Yang via R-help <r-help at r-project.org> wrote:> > Hello R team,I want to use for loop to generate multiple plots with 3 parameter, (y is for y axis, c is for color and f is for file name in output). I created a data frame to save the information and use the information in for loop. I use y[i], c[i] and f[i] in the loop, but it seems doesn't work. Can anyone correct my code to make it work? > Thanks,Kai > > library(ggplot2)library(tidyverse) > y <- c("hwy","cty")c <- c("cyl","class")f <- c("hwy_cyl","cty_class") > mac <- data.frame(y,c,f) > for (i in nrow(mac)){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point() ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units = "in")} > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Wed, 22 Dec 2021 16:58:18 +0000 (UTC) Kai Yang via R-help <r-help at r-project.org> wrote:> mpg %>%? ? filter(hwy <35) %>%?? ? ggplot(aes(x = displ, y = y[i], > color = c[i])) +?? ? geom_point()Your code relies on R's auto-printing, where each line of code executed at the top level (not in loops or functions) is run as if it was wrapped in print(...the rest of the line...). Solution: make that print() explicit. A better solution: explicitly pass the plot object returned by the ggplot functions to the ggsave() function instead of relying on the global state of the program.> ggsave("c:/temp/f[i].jpg",width = 9, height = 6, dpi = 1200, units > "in")When you type "c:/temp/f[i].jpg", what do you get in return? Use paste0() or sprintf() to compose strings out of parts.> [[alternative HTML version deleted]]P.S. Please compose your messages in plain text, not HTML. See the R-help posting guide for more info. -- Best regards, Ivan