0

I would like to loop through each factor (cell) level and make a separate plot. My code makes separate plots but I do not know how to add these into one figure...

My data: https://www.dropbox.com/s/cczbbka0xxj57z5/df.csv?dl=0

The code I am working with:

windows(width=18,height=10)
a=layout(matrix(c(1,2,3,4), nrow = 2, ncol = 2, byrow=T))
layout.show(a)

# create a list of cells in data to loop over 

cell_list <- unique(df$cell)

# create for loop to produce ggplot2 graphs for each cell

for (i in seq_along(cell_list)) { 

# create a plot for each cell 
  plot <- 
    ggplot(subset(df, df$cell == cell_list[i]),

           aes(x=condition, y=test_variable, group = cell)) +

    labs(x = "Cell", y = "test_variable") +

    ggtitle(unique(df$cell)[i]) +

    geom_line(size = 1)+
    geom_point(size=2, shape=17) +  

    theme(legend.position = "none") 

  print(plot)
}
user7395965
  • 397
  • 5
  • 17
  • skip the loop and use `facet_wrap(~cell)`? – Robin Gertenbach Dec 29 '19 at 20:57
  • 1
    If you don't want to do this you can store them into a list, and then use `grid.arrange()` as described here https://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange – Kreuni Dec 29 '19 at 21:28
  • Instead of sending a link to a dropbox account where we can't download your dataset without having to sign in Dropbox, can you provide a reproducible example of your data as described here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Dec 29 '19 at 22:28
  • `facet_wrap` and `facet_grid` are certainly great solutions for someone like me who does not know how to write loops well... I would be still curious to learn how to store the plots automatically into a list and then plot them. – user7395965 Dec 29 '19 at 23:32

1 Answers1

3

As mentioned by @Robin Gertenbach, you can have the use of the function facet_wrap or facet_grid.

library(ggplot2)
ggplot(df, aes(x = condition, y = test_variable, group = cell, color = cell)) +
  geom_point()+
  geom_line()+
  scale_x_discrete(limits = c("before1","before2","after1","after2","after3"))+
  facet_wrap(.~cell)

enter image description here

If you want to have all your graph aligned in the same row, you can use facet_grid(.~cell) (and if you want as a single column, you can do the opposite facet_grid(cell~.))

library(ggplot2)
ggplot(df, aes(x = condition, y = test_variable, group = cell, color = cell)) +
  geom_point()+
  geom_line()+
  scale_x_discrete(limits = c("before1","before2","after1","after2","after3"))+
  facet_grid(cell~.)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

If you don't want to keep the same scale between plot, you can add scales = free in both facet_wrap or facet_grid.

Does it answer your question ?

Data

structure(list(cell = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L), .Label = c("10b", 
"13a", "1b", "5a"), class = "factor"), condition = structure(c(4L, 
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L), .Label = c("after1", "after2", "after3", "before1", 
"before2"), class = "factor"), test_variable = c(58L, 55L, 36L, 
29L, 53L, 57L, 53L, 54L, 52L, 52L, 40L, 42L, 30L, 25L, 28L, 50L, 
49L, 66L, 69L, 64L)), class = "data.frame", row.names = c(NA, 
-20L))
dc37
  • 15,840
  • 4
  • 15
  • 32