将alpha图例添加到ggplot2散点图中

时间:2015-08-20 17:14:02

标签: r ggplot2

我的数据看起来像这样:

DF <- data.frame(
  x = rnorm(100, mean = 0, sd = 1),
  y = rnorm(n = 100, mean = 1, sd = 1),
  color = sample(
    x = c('red', 'blue'), size = 100, replace = T, prob = c(.5,.5)
  ),
  alpha = runif(n = 100, min = 0.1, max = 1)
)

我想生成散点图,我希望图表上唯一的图例为alpha,这是一个连续变量,可以取0.1到1之间的值。

这就是我现在所拥有的:

DF %>%
  ggplot(aes(
    x = x, y = y, color = color, size = 2, alpha = alpha
  )) +
  geom_point(show_guide = TRUE) + theme_bw() 

这可能吗?

2 个答案:

答案 0 :(得分:2)

试试这个:

DF %>%
    ggplot(aes(x = x, y = y, color = color, alpha = alpha),size = 2) +
    geom_point() + 
    guides(color = "none") +
    theme_bw() 

答案 1 :(得分:0)

我无法将alpha作为连续缩放,但colorsize的图例可以通过将其指南设置为FALSE来删除。

DF %>%
    ggplot(aes(
        x = x, y = y, color = color, size = 2, alpha = alpha
    )) +
    geom_point(show_guide = TRUE) + theme_bw() + 
scale_size(guide = FALSE) + 
scale_color_discrete(guide = FALSE)