I would like to use two color palettes on the same plot. I have two groups and different modalities inside and my goal is each group has its own color palettes
i don t
Note: the suggested approaches below are feasible when the number of factors within each group is reasonably small. If there are many different group-factor combinations, your audience may not be able to tell the difference between small changes in colour, in which case faceting by group could allow for a clearer & cleaner picture.
(My own working assumption for any presentation is to assume that the projector is >10 years old, the printer ran out of ink, & at least half the target audience are overdue for new prescription lenses...)
# sample data
set.seed(10)
df <- data.frame(side = c(rep("Left", 3), rep("Right", 3), rep("Left", 3), rep("Right", 3)),
factor = c(rep("A", 6), rep("B", 6)),
time = rep(seq(1, 3), 4),
value = sample(seq(1, 10), 12, replace = T))
Approach 1: vary colour
by group & vary alpha
by factor
ggplot(df,
aes(x = time, y = value, colour = side, alpha = factor)) +
geom_line(size = 3) +
scale_alpha_discrete(name = "Factor", range = c(0.5, 1)) +
scale_color_manual(name = "Side", values = c("#1f78b4", "#33a02c")) +
theme_classic()
This works best when the lines do not overlap too much, since the overlapped regions look darker.
Approach 2: (for 2 factors only) create a combined variable & use "Paired" palette
ggplot(df %>%
mutate(combined.variable = factor(paste(side, factor, sep = "~"))),
aes(x = time, y = value, colour = combined.variable)) +
geom_line(size = 3) +
scale_color_brewer(name = "Side~Factor", palette = "Paired") +
theme_classic()
RColorBrewer
package's "Paired" palette has 12 colours in (roughly) 6 pairs. If you have <=6 groups & 2 factors in each group, this could be a easy way to assign the colours. Here's a colour wheel for reference: