0

I am trying to plot multiple plots from each data stored in the list.My code works when I am trying to plot for a single dataframe in a list:

ggplot(data.frame(s[1]), aes(x = No, y = Val))  +
  geom_line(linetype = "dashed",color="red")+geom_point()

but unable to work it when I am running a loop.

for (i in 1:3)
{
  ggplot(data.frame(s[i]), aes(x = No, y = Val))  +
    geom_line(linetype = "dashed",color="red")+geom_point()
  i = i+1
}

How to resolve the issue?

1
  • 1
    Could you expand on how it doesn't work? Do the plots not show? Do the data look wrong? Would you like to save the plots in a list? Do you get errors? Commented Nov 21, 2019 at 7:41

2 Answers 2

1

You can save the plot within the for loop on a variable and print as follows:

for (i in 1:3)
{
  myplot <- ggplot(data.frame(s[i]), aes(x = No, y = Val))  +
    geom_line(linetype = "dashed",color="red")+geom_point()
  print(myplot)

}
Sign up to request clarification or add additional context in comments.

Comments

0

You could also store all plots in a list

myplot <- list()
for (i in 1:3) {
   myplot[i] <- ggplot(data.frame(s[i]), aes(x = No, y = Val)) +
   geom_line(linetype = "dashed", color = "red") + 
   geom_point()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.