R ggplot two color palette on the same plot

2020-05-10 06:39发布

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 manage to do that with scale_colour_brewer

could anyone help me.

cheers


My topics is actually broad. I am going to specify.

I can sum up my data as follow :

| side | factor  | Time | value |
|------|---------|------|-------|
| Left | methodA | 0    | 5     |
| Left | methodA | 1    | 3     |
| Left | methodA | 2    | 2     |
| Right| methodA | 2    | 2     |
| Right| methodA | 2    | 2     |
| Right| methodA | 2    | 2     |
| Left | methodB | 0    | 5     |
| Left | methodB | 1    | 3     |
| Left | methodB | 2    | 2     |
| Right| methodB | 2    | 2     |
| Right| methodB | 2    | 2     |
| Right| methodB | 2    | 2     |

i would like to plot a line of Value against Time - use a red color palette for Side == Left with factor represented by a red variation - use a blue color palette for Side == Right with factor represented by a blue variation

cheers

标签: r ggplot2
1条回答
戒情不戒烟
2楼-- · 2020-05-10 07:00

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()

Approach 1

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()

Approach 2

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:

colour wheel

查看更多
登录 后发表回答