Re: [R] for loop question in R
Hi Rui and Ivan,Thank you explain of the code for me in detail. This is very helpful. And the code works well now.Happy Holiday,Kai On Wednesday, December 22, 2021, 02:30:49 PM PST, Rui Barradas wrote: Hello, y[i] and c[i] are character strings, they are not variables of data set mpg. To get the variables, use, well, help("get"). Note that I have changed the temp dir to mine. So I created a variable to hold the value tmpdir <- "c:/temp/" for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) ggsave( paste0(tmpdir, f[i], ".jpg"), width = 9, height = 6, dpi = 1200, units = "in") } Like Ivan said, don't rely on auto print. In order to have to open the graphics files output by the loop I would have done something like the following. First create a list to hold the plots. Inside the for loop save the plots in the list and explicitly print them. And use ggsave argument plot. Like this, after the loop you can see what you have by printing each list member. p <- vector("list", length = nrow(mac)) for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) -> p[[i]] ggsave( paste0(tmpdir, f[i], ".jpg"), plot = p[[i]], width = 9, height = 6, dpi = 1200, units = "in") } # See the first plot p[[1]] Hope this helps, Rui Barradas Às 18:18 de 22/12/21, Kai Yang via R-help escreveu: > Hello Eric, Jim and Ivan, > Many thanks all of your help. I'm a new one in R area. I may not fully > understand the idea from you. I modified my code below, I can get the plots > out with correct file name, but plots are not using correct fields' name. it > use y[i], and c[i] as variables' name, does not use hwy, cyl or cty, class in > ggplot statement. And there is not any error message. Could you please look > into my modified code below and let me know how to modify y= y[i], color = > c[i] part? > Thanks, > Kai > > y <- c("hwy","cty") > c <- c("cyl","class") > f <- c("hwy_cyl","cty_class") > mac <- data.frame(y,c,f) > for (i in seq(nrow(mac))){ > mpg %>% > filter(hwy <35) %>% > ggplot(aes(x = displ, y = y[i], color = c[i])) + > geom_point() > ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, >units = "in") > } > > On Wednesday, December 22, 2021, 09:42:45 AM PST, Ivan Krylov > wrote: > > On Wed, 22 Dec 2021 16:58:18 + (UTC) > Kai Yang via R-help 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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] for loop question in R
Hello, There's a stupid typo in my previous post. Inline Às 22:30 de 22/12/21, Rui Barradas escreveu: Hello, y[i] and c[i] are character strings, they are not variables of data set mpg. To get the variables, use, well, help("get"). Note that I have changed the temp dir to mine. So I created a variable to hold the value tmpdir <- "c:/temp/" for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) ggsave( paste0(tmpdir, f[i], ".jpg"), width = 9, height = 6, dpi = 1200, units = "in") } Like Ivan said, don't rely on auto print. In order to have to open the Should read In order to *avoid* to open the Rui Barradas graphics files output by the loop I would have done something like the following. First create a list to hold the plots. Inside the for loop save the plots in the list and explicitly print them. And use ggsave argument plot. Like this, after the loop you can see what you have by printing each list member. p <- vector("list", length = nrow(mac)) for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) -> p[[i]] ggsave( paste0(tmpdir, f[i], ".jpg"), plot = p[[i]], width = 9, height = 6, dpi = 1200, units = "in") } # See the first plot p[[1]] Hope this helps, Rui Barradas Às 18:18 de 22/12/21, Kai Yang via R-help escreveu: Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I may not fully understand the idea from you. I modified my code below, I can get the plots out with correct file name, but plots are not using correct fields' name. it use y[i], and c[i] as variables' name, does not use hwy, cyl or cty, class in ggplot statement. And there is not any error message. Could you please look into my modified code below and let me know how to modify y= y[i], color = c[i] part? Thanks, Kai y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in seq(nrow(mac))){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point() ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, units = "in") } On Wednesday, December 22, 2021, 09:42:45 AM PST, Ivan Krylov wrote: On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help 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. __ R-help@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. __ R-help@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.
Re: [R] for loop question in R
Hello, y[i] and c[i] are character strings, they are not variables of data set mpg. To get the variables, use, well, help("get"). Note that I have changed the temp dir to mine. So I created a variable to hold the value tmpdir <- "c:/temp/" for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) ggsave( paste0(tmpdir, f[i], ".jpg"), width = 9, height = 6, dpi = 1200, units = "in") } Like Ivan said, don't rely on auto print. In order to have to open the graphics files output by the loop I would have done something like the following. First create a list to hold the plots. Inside the for loop save the plots in the list and explicitly print them. And use ggsave argument plot. Like this, after the loop you can see what you have by printing each list member. p <- vector("list", length = nrow(mac)) for (i in seq(nrow(mac))){ mpg %>% filter(hwy < 35) %>% ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]))) + geom_point() + ylab(y[i]) + guides(color = guide_legend(title = c[i])) -> p[[i]] ggsave( paste0(tmpdir, f[i], ".jpg"), plot = p[[i]], width = 9, height = 6, dpi = 1200, units = "in") } # See the first plot p[[1]] Hope this helps, Rui Barradas Às 18:18 de 22/12/21, Kai Yang via R-help escreveu: Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I may not fully understand the idea from you. I modified my code below, I can get the plots out with correct file name, but plots are not using correct fields' name. it use y[i], and c[i] as variables' name, does not use hwy, cyl or cty, class in ggplot statement. And there is not any error message. Could you please look into my modified code below and let me know how to modify y= y[i], color = c[i] part? Thanks, Kai y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in seq(nrow(mac))){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point() ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, units = "in") } On Wednesday, December 22, 2021, 09:42:45 AM PST, Ivan Krylov wrote: On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help 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. __ R-help@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.
Re: [R] for loop question in R
strange, I got error message when I run again: Error: unexpected symbol in: " geom_point() ggsave" > } Error: unexpected '}' in "}" On Wednesday, December 22, 2021, 10:18:56 AM PST, Kai Yang wrote: Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I may not fully understand the idea from you. I modified my code below, I can get the plots out with correct file name, but plots are not using correct fields' name. it use y[i], and c[i] as variables' name, does not use hwy, cyl or cty, class in ggplot statement. And there is not any error message. Could you please look into my modified code below and let me know how to modify y= y[i], color = c[i] part? Thanks, Kai y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in seq(nrow(mac))){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point() ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, units = "in") } On Wednesday, December 22, 2021, 09:42:45 AM PST, Ivan Krylov wrote: On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help 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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] for loop question in R
Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I may not fully understand the idea from you. I modified my code below, I can get the plots out with correct file name, but plots are not using correct fields' name. it use y[i], and c[i] as variables' name, does not use hwy, cyl or cty, class in ggplot statement. And there is not any error message. Could you please look into my modified code below and let me know how to modify y= y[i], color = c[i] part? Thanks, Kai y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in seq(nrow(mac))){ mpg %>% filter(hwy <35) %>% ggplot(aes(x = displ, y = y[i], color = c[i])) + geom_point() ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, units = "in") } On Wednesday, December 22, 2021, 09:42:45 AM PST, Ivan Krylov wrote: On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help 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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] for loop question in R
On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help 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 __ R-help@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.
Re: [R] for loop question in R
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 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@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. __ R-help@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.
Re: [R] for loop question in R
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 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@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]] __ R-help@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.
Re: [R] for loop question in R
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 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@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]] __ R-help@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.
[R] for loop question in R
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@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.