Displaying R plots in a 1x4 grid, using a shared y-axis

Viewed 83

I am trying to display some graphs in a 1x4 grid, but I would like all the graphs to have the same x and y axes.

time maxhgs.sleep_LIPA maxhgs.sed_LIPA maxhgs.stand_LIPA maxhgs.MVPA_LIPA maxhgs.LIPA_MVPA
1    5        0.08289621      0.03241295         0.1129983      0.112998341      -0.01928050
2   10        0.16289049      0.06139545         0.2236818     -0.006728721      -0.04950022
3   15        0.24025861      0.08721203         0.3323473     -0.047756360      -0.08927656
4   20        0.31524160      0.11009218         0.4392581     -0.144261526      -0.13791276
5   25        0.38805152      0.13023596         0.5446498     -0.424789999      -0.19517306
6   30        0.41660977      0.13756729         0.5864293     -0.934884300      -0.26117695

This is the data I am working with.

library(ggplot2)
library(egg)

maxhgs.a <- ggplot(maxhgs.df, aes(time, maxhgs.sleep_LIPA)) + geom_point()+geom_line()
maxhgs.a <- maxhgs.a + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Sleep to LIPA")

maxhgs.b <- ggplot(maxhgs.df, aes(time, maxhgs.sed_LIPA)) + geom_point()+geom_line()
maxhgs.b <- maxhgs.b + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Sedentary to LIPA")

maxhgs.c <- ggplot(maxhgs.df, aes(time, maxhgs.stand_LIPA)) + geom_point()+geom_line()
maxhgs.c <- maxhgs.c + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
  ggtitle("Standing to LIPA")

maxhgs.d <- ggplot(maxhgs.df, aes(time, maxhgs.MVPA_LIPA)) + geom_point()+geom_line()
maxhgs.d <- maxhgs.d + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
  scale_y_continuous(name = "Change in maxhgs", breaks = seq(0.5,-1, by=-0.1))+
  ggtitle("MVPA to LIPA")


ggarrange(maxhgs.a, 
          maxhgs.b + 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), 
          maxhgs.c + 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ),
          maxhgs.d +
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), 
          nrow = 1)

This is what I have attempted so far. This actually "works" in that all the graphs have the same y-axis, but the y-axis doesn't actually reflect what should be on the graphs. As you can see in the graph, the y-axis goes from 0.1 to 0.4, but the maxhgs.d graph should extend from 0.1 to -0.9.

enter image description here

Any advice or suggestions would be greatly appreciated!

1 Answers

You can make this much easier by reshaping your data and using faceting. That way, you only need to define a single plot. This requires you to pivot_longer and change the factor levels to the names you want for each facet, but once this is done, the plot itself is straightforward:

library(ggplot2)
library(dplyr)

# Define the label names for the facets first
labs <- c("LIPA to MVPA", "MVPA to LIPA", "Sedentary to LIPA",
          "Sleep to LIPA", "Standing to LIPA")

gg <- maxhgs.df %>% 
  tidyr::pivot_longer(cols = -1) %>% 
  mutate(plot = factor(`levels<-`(factor(name), labs), labs[c(4, 3, 5, 2, 1)])) %>%
  ggplot(aes(x = time, y = value)) + 
  geom_line() +
  geom_point() +
  scale_x_continuous(name = "Time Reallocated") +
  scale_y_continuous(name = "Change in maxhgs") +
  theme(strip.background = element_blank(),
        strip.text = element_text(size = 13)) 

Now we can either choose to plot with fixed y axes:

gg + facet_grid(.~plot, scale = "fixed")

enter image description here

or with flexible y axes:

gg + facet_wrap(.~plot, scale = "free_y", ncol = 5)

enter image description here

Created on 2020-08-04 by the reprex package (v0.3.0)

Related