More compact use of ggplot : grid spaghetti plot

466 views Asked by At

The following code plot the predicted probability of several models against time. Having, all the plots on one graph was not readable so I divided the result in a grid.

I was wondering if it was possible to have only one ggplot with all the models then somehow specify which goes where with grid.arrange

Current :

p2.dat1 <- select(ppf, EXPOSURE, predp.glm.gen,predp.glm1, predp.glm2,predp.glm3,predp.glm4 )
mdf1 <- melt(p2.dat1 , id.vars="EXPOSURE")
plm.plot.all1 <- ggplot(data = mdf1,
                                             aes(x = EXPOSURE, y = value, colour = variable)) +
                                             geom_line()

p2.dat2 <- select(ppf, EXPOSURE, predp.glm.gen, predp.glm5,predp.glm.step )
mdf2 <- melt(p2.dat2 , id.vars="EXPOSURE")
plm.plot.all2 <- ggplot(data = mdf2,
                                                aes(x = EXPOSURE, y = value, colour = variable)) +
                                                geom_line() 

grid.arrange(plm.plot.all1, plm.plot.all2, nrow=2)

Expected:

p2.dat <- select(ppf, EXPOSURE, predp.glm.gen,predp.glm1, predp.glm2,predp.glm3,predp.glm4,predp.glm5,predp.glm.step)
mdf <- melt(p2.dat , id.vars="EXPOSURE")
plm.plot.all <- ggplot(data = mdf1,
                                             aes(x = EXPOSURE, y = value, colour = variable)) +
                                             geom_line()

grid.arrange(plm.plot.all[some_selection_somehow], plm.plot.all[same], nrow=2)

Thanks,

1

There are 1 answers

1
Jake Kaupp On

You can do this with grid.arrange by writing some helper functions. It can be done more succinctly, but I prefer small focused functions that can be used with pipes.

library(tidyverse)
library(gridExtra)

# Helper Functions ----
plot_function <- function(x) {

  ggplot(x, aes(x = EXPOSURE, y = value, colour = variable)) +
    geom_line() +
    labs(title = unique(x$variable)) +
    theme(legend.position = "none")
}

grid_plot <- function(x, selection) {

  order <- c(names(x)[grepl(selection,names(x))], names(x)[!grepl(selection,names(x))])

  grid.arrange(grobs = x[order], nrow = 2)
}

# Actually make the plot ----
ppf %>% 
  select(EXPOSURE, predp.glm.gen,predp.glm1, predp.glm2,predp.glm3,predp.glm4,predp.glm5,predp.glm.step) %>% 
  gather(variable, value, -EXPOSURE) %>% 
  split(.$variable) %>% 
  map(plot_function) %>% 
  grid_plot("predp.glm3")

enter image description here

or you could do this with ggplot, a facet_wrap and factoring the variable column to the proper order. This has the benefits of shared axes across the plots, which facilitates easy comparison. You can alter the helper functions in the first approach to set the axes explicitly to achieve the same effect, but its just easier keeping it in ggplot.

library(tidyverse)

selection <- "predp.glm3"

plot_data <- ppf %>% 
  select(EXPOSURE, predp.glm.gen,predp.glm1, predp.glm2,predp.glm3,predp.glm4,predp.glm5,predp.glm.step) %>% 
  gather(variable, value, -EXPOSURE) %>% 
  mutate(variable = fct_relevel(variable, c(selection, levels(variable)[-grepl(selection, levels(variable))])))

ggplot(plot_data, aes(x = EXPOSURE, y = value, colour = variable)) +
  geom_line() +
  facet_wrap( ~variable, nrow = 2) +
  theme(legend.position = "none")

enter image description here