0
votes

I'm trying to plot multiple bar and scatter plots tightly together (i.e. without gaps separating the plots). I'm plotting flow and salinity values for two different rivers, and it's important that the years are aligned although one had data for one year earlier/one year later. I want there to be no gaps, axes tick labels, axis labels, spaces between the plots, though tick marks for each plot would help make it readable. I also would like there to be very light grey vertical lines throughout, to make it easy to compare the different values for the two rivers for a given year.

I've tried unsuccessfully to do this in both R base plan and ggplot2, and can't figure it out. I can't find a simple way to align the x-axes (they don't align across from point to bar plots, and from river to river), nor to get the plots to sit vertically adjacent.

I will want to export the output as a multi-panel .png image.

Here's example data and how I've tried to do it so far in ggplot:

river1 = data.frame(year     =  1991:2017, 
                    flow     =  rnorm(n=27, mean = 900, sd = 350),
                    salinity =  rnorm(n=27, mean = 3000, sd = 800))
river2 = data.frame(year     =  1992:2019, 
                    flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                    salinity =  rnorm(n=28, mean = 700, sd = 1500))

library(ggplot2)
(r1sal = ggplot(river1, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 1 flow and flow-weighted salinity", x = "", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r1flow = ggplot(river1, aes(x = year, y = flow)) + 
    geom_bar(stat = "identity", color="blue", fill="lightblue") + 
    labs(x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2sal = ggplot(river2, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 2 flow and flow-weighted salinity", x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2flow = ggplot(river2, aes(x=year, y=flow)) + 
       geom_bar(stat = "identity", color="blue", fill="lightblue") + 
       labs( x = "Water year", y = "Streamflow (ML)") +
       scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

library(ggpubr)
ggarrange(r1sal, r1flow, r2sal, r2flow,
            labels = c("A", "B", "C","D"),
            ncol = 1, nrow = 4)

I know there must be a simple way to do this. A big thanks for any assistance!

1

1 Answers

1
votes

I would consider using facets.

river1 = data.frame(year     =  1991:2017, 
                    flow     =  rnorm(n=27, mean = 900, sd = 350),
                    salinity =  rnorm(n=27, mean = 3000, sd = 800))
river2 = data.frame(year     =  1992:2019, 
                    flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                    salinity =  rnorm(n=28, mean = 700, sd = 1500))


library(tidyverse)

rivers <- bind_rows(river1 = river1, river2 = river2, .id = "River") %>% 
  pivot_longer(cols = c("flow", "salinity"), names_to = "variable", values_to = "value") 

ggplot(data = filter(rivers, variable == "salinity" ), mapping = aes(x = year, y = value)) +
  geom_col(data = filter(rivers, variable == "flow" ), fill = "blue") +
  geom_point(color="darkred") +
  geom_line(color="darkred") +
  facet_grid(River + variable ~ ., scales = "free_y")

Created on 2020-05-11 by the reprex package (v0.3.0)

The alternative would be to play with themes to set axis.text to element_blank() and then combine the figures (I would do this with patchwork package)