2

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?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Paul Wild
  • 91
  • 1
  • 8
  • 2
    Have a look at [ggnewscale](https://eliocamp.github.io/ggnewscale/) which allows to add multiple scales for the same aes. – stefan Jul 14 '22 at 22:58
  • @stefan - I have been trying lately to connect those multiple scales for one aesthetic threads to the linked thread - I have added a wiki answer to that. For the next time you'll come across such a question (it will be soon :) – tjebo Jul 15 '22 at 08:41

1 Answers1

2

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)

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Perfect. Thank you so much. I had no idea that this package even existed. There are so many different packages and ways to complete the same task that sometimes I feel like I don't know what I don't know. Thank you for your help. – Paul Wild Jul 19 '22 at 16:30