[R] for loop question in R

Kai Yang y@ngk@|9999 @end|ng |rom y@hoo@com
Wed Dec 22 17:58:18 CET 2021


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]]



More information about the R-help mailing list