R ggplot同一图上的两个调色板 [英] R ggplot two color palette on the same plot

查看:182
本文介绍了R ggplot同一图上的两个调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一图上使用两个调色板. 我有两个小组,每个小组有不同的方式,我的目标是每个小组都有自己的调色板

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

我没办法用scale_colour_brewer做到这一点

i don t manage to do that with scale_colour_brewer

有人可以帮助我吗?

欢呼

我的话题实际上很广泛. 我要指定.

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

我可以总结如下数据:

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

我想针对时间绘制一条价值线 -对Side == Left使用红色调色板,其因子由红色变化表示 -为Side == Right使用蓝色调色板,并使用蓝色变化表示因子

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

欢呼

推荐答案

注意:当每个组中的因素数量合理地小时,以下建议的方法是可行的.如果有许多不同的群体因素组合,您的听众可能无法分辨出颜色的细微变化之间的差异,在这种情况下,按群体进行构面可以使画面更清晰,更清晰.清晰的图片.

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.

(对于任何演示文稿,我自己的工作假设是假设投影机的使用年限已超过10年,打印机的墨水用完了,并且至少有一半的目标受众已逾期购买新的处方镜片... )

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

方法1 :根据组& alpha因因素而异

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.

方法2 :(仅针对2个因素)创建组合变量&使用配对"调色板

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程序包的"Paired"调色板具有(大约)6对中的12种颜色.如果您有< = 6个组&每组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:

这篇关于R ggplot同一图上的两个调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆