[R] for loop question in R

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Dec 22 23:43:10 CET 2021


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 
>> <krylov.r00t using gmail.com> wrote:
>>   On Wed, 22 Dec 2021 16:58:18 +0000 (UTC)
>> Kai Yang via R-help <r-help using 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.
>>
> 
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list