Can I combine two colored datasets in R with ggplot2 and maintain their colors on the combined graph?

Viewed 38

I have multiple, large sets of data that changes over time. I am subsetting them and graphing them on a 0 - 24 hour time scale, with each day of the week getting a different shade of the same color using colorbrewer - see the attached graphs.

Example of the first finished graph. Example of the second finished graph.

I would like to combine two of these graphs and maintain their original color brewer shades. To graph each dataset individually I have been doing the following:

pb <- ggplot(PBSub2, aes(x= STH, y = pH, group = Day, color = Day)) +
geom_line(size=1)+
theme_dark()+
ggtitle("TITLE")+
xlab("Standing time (hours)")+
scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
scale_y_continuous(limits = c(6,8))
pb + scale_color_brewer(,,palette = "Reds")

p<- ggplot(WISub1, aes(x= STH, y = pH, group = Day, color = Day)) +
geom_line(size=1)+
theme_dark()+
ggtitle("TITLE")+
xlab("Standing time (hours)")+
scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
scale_y_continuous(limits = c(6,8))
p + scale_color_brewer(,,palette = "Blues")

I figured out how to combine the data into the same graph, with the following code, but I cannot figure out how to separate them into their original "Reds" and "Blues" colors:

ggplot(NULL, aes(x= STH, y = pH, group = Day, color = Day)) +
geom_line(data=WISub1, size=1, color = WISub1$Day)+
scale_color_brewer(,,palette = "Blues")+
geom_line(data=PBSub2, size=1, color = PBSub2$Day)+
scale_color_brewer(,,palette = 'Reds')+
theme_dark()+
ggtitle("TITLE")+
xlab("Standing time (hours)")+
scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
scale_y_continuous(limits = c(6,8))

The second "scale_color_brewer" overrides the first instead of only applying to the second element. Should I manually assign colors? Or is there an automatic way to assign the same colors?

1 Answers

As suggested by @stefan, here is an example using the ggnewscale package and some 'example' data:

library(tidyverse)
library(ggnewscale)

PBSub2 <- data.frame(STH = seq(0, 24, length.out = 175),
                     pH = rnorm(175, 7.5, 0.25),
                     Day = factor(16:22, levels = 16:22))

WISub1 <- data.frame(STH = seq(0, 24, length.out = 175),
                     pH = rnorm(175, 6.5, 0.25),
                     Day = factor(9:15, levels = 9:15))

pb <- ggplot(PBSub2, aes(x= STH, y = pH, group = Day, color = Day)) +
  geom_line(size=1)+
  theme_dark()+
  ggtitle("TITLE")+
  xlab("Standing time (hours)")+
  scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
  scale_y_continuous(limits = c(6,8))
pb + scale_color_brewer(,,palette = "Reds")


p<- ggplot(WISub1, aes(x= STH, y = pH, group = Day, color = Day)) +
  geom_line(size=1)+
  theme_dark()+
  ggtitle("TITLE")+
  xlab("Standing time (hours)")+
  scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
  scale_y_continuous(limits = c(6,8))
p + scale_color_brewer(,,palette = "Blues")
#> Warning: Removed 1 row(s) containing missing values (geom_path).


combined <- ggplot() +
  geom_line(data = PBSub2, 
            aes(x= STH, y = pH, group = Day, color = Day),
            size=1)+
  theme_dark()+
  ggtitle("TITLE")+
  xlab("Standing time (hours)")+
  scale_x_continuous(breaks=c(0,4,8,12,16,20,24))+
  scale_y_continuous(limits = c(6,8)) +
  scale_color_brewer(,,palette = "Reds") +
  ggnewscale::new_scale_color() +
  geom_line(data = WISub1, 
            aes(x= STH, y = pH, group = Day, color = Day),
            size=1)+
  scale_color_brewer(,,palette = "Blues")
combined
#> Warning: Removed 1 row(s) containing missing values (geom_path).

Created on 2022-07-15 by the reprex package (v2.0.1)

Related