-1

I am looking to create a loop (if possible!) whereby this code:

ggplot(Sphere_Run1_Call_2019, aes(x=Time..Seconds.)) + 
  geom_line(aes(y = Accx), color = "darkred") + 
  geom_line(aes(y = Accy), color="steelblue", linetype="solid") + 
  geom_line(aes(y = Accz), color="green", linetype="solid") + 
  theme_classic() + 
  ggtitle("Run 1") + 
  xlab("Time (Seconds)") + 
  ylab("Acceleration (m/s2)")

is repeated through 9 more dataframes (Sphere_Run2_Call_2019, Sphere_Run3_Call_2019, etc...) to generate 10 graphs i can export.

is this possible in ggplot? and is Looping best in this instance?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Crabbe1
  • 5
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 18 '19 at 21:30
  • If your all your dataframes have similar structure you could append them in a list and map ggplot function over them, example: mtcars1 <- sample_frac(mtcars,0.5) table_list <- list(dataframe1 = mtcars1,dataframe2 = mtcars) map(table_list,~ggplot(.,aes(mpg,disp))+ geom_line()) – Yazid Oct 18 '19 at 23:14

1 Answers1

0

You could use something like:

for(i in seq(10)){
  ggplot(get(paste("Sphere_Run",i,"_Call_2019",sep="")), 
    aes(x=Time..Seconds.)) +
    geom_line(aes(y = Accx), color = "darkred") +
    geom_line(aes(y = Accy), color="steelblue", linetype="solid") +
    geom_line(aes(y = Accz), color="green", linetype="solid") +
    theme_classic() +
    ggtitle("Run 1") +
    xlab("Time (Seconds)") +
    ylab("Acceleration (m/s2)")
}
Diego Rodrigues
  • 824
  • 4
  • 10