在ggplot2中绘制较大点和较小点较暗时合并图例

时间:2014-10-05 19:08:14

标签: r ggplot2

我已经删除了ggplot2文档,Stack和ggplot2 Google网上论坛电子邮件列表 - 但无济于事。

请有人告诉我如何合并alpha(不透明度)和size的图例?它们的标题是"(1-val2)"和" val2"分别。

通常将alpha和size映射到val2会自动合并轴。但是因为我正在使用" val2"和" 1-val2",这不会发生。我曾与scale_size_continuousscale_alpha_continuous玩过,但没有成功。

这是一个MWE:

require(ggplot2)

dummy <- data.frame(x=c(runif(12,5,10)),
                y=c(runif(12,5,10)),
                val1=c("a","b","c","a","b","c","a","b","c","a","b","c"),
                val2=c(0.4,0.6,0.7,0.2,0.8,0.6,0.7,0.2,0.5,0.8,0.4,0.7))

p <- ggplot() +
geom_point(data=dummy, aes(x=x, y=y,color=val1, size=val2, alpha=(1-val2))) 

2 个答案:

答案 0 :(得分:2)

使用range的{​​{1}}参数反转比例:

scale_alpha_continuous

resulting plot

答案 1 :(得分:2)

trans参数在这里也可能有用:

ggplot() +
  geom_point(data=dummy, aes(x = x, y = y, color = val1, size = val2, alpha = val2)) +
  scale_alpha_continuous(trans = "reverse")

enter image description here

trans?scale_alpha_continuous中的?continuous_scale参数说明非常简短。但是,您可以找到一些示例here