首页 > 解决方案 > ggplot中的因子颜色和连续变量

问题描述

我正在尝试使用颜色来突出因子水平之间和内部的差异。例如,使用这些可重现的数据:

set.seed(123)
dat <- data.frame(
  Factor = sample(c("AAA", "BBB", "CCC"), 50, replace = T),
  ColorValue = sample(1:4, 50 , replace = T),
  x = sample(1:50, 50, replace =T),
  y = sample(1:50, 50, replace =T))
head(dat)

  Factor ColorValue  x  y
1    AAA          1 30 43
2    CCC          2 17 25
3    BBB          4 25 20
4    CCC          1 48 13
5    CCC          3 25  6
6    AAA          1 45 20

我想为每个因素使用不同的颜色。然后,在每个因素中,我试图将ColorValue其用作连续着色变量来显示强度。

在下图中,每个方面将具有反映 的不同深浅的红色、绿色和蓝色ColorValue,理想情况下,所有三个因子水平都具有单个强度(即 ColorValue)图例。

 ggplot(dat, aes(x = x, y = y, color = Factor)) +
  geom_point(size = 3) +
  facet_wrap(~Factor) + 
  theme_bw()

在此处输入图像描述

标签: rggplot2

解决方案


 ggplot(dat, aes(x = x, y = y, color = Factor, alpha = ColorValue)) +
  geom_point(size = 3) +
  facet_wrap(~Factor) + 
  theme_bw()

结果


推荐阅读