有没有办法为ggplot的alpha添加图例? [英] Is there a way to add a legend for ggplot's alpha?

查看:157
本文介绍了有没有办法为ggplot的alpha添加图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有很多重叠点的图(从2-10开始)。将抖动添加到点使其非常嘈杂和不吸引人。我喜欢在美学中添加一个阿尔法。不过,我希望有一个图例,读者可以看到每个透明胶片有多少点重叠。有没有这样的方法?

  ggplot(data = mydata,aes(x = x,y = y))+ geom_point( size = 3,shape = 2,aes(alpha = 1/3))

上面的代码。如何为alpha添加图例?

解决方案

下面是一个如何伪造这个的例子。尝试它几次,同时改变 alpha

  require(ggplot2) 

n = 10000
alpha = 0.01

set.seed(12345)
data = data.frame(replicate(2,rnorm(n)) )

dev.new(width = 4,height = 3)
p = qplot(X1,X2,data = data,alpha = alpha)

fake_scale = scale_alpha('重叠pts'的数目,breaks = pretty(c(alpha,1)),labels = as.character(pretty(c(alpha,1))/ alpha))

p + fake_scale

alpha = 0.1





alpha = 0.01




I have a plot with many overlapping points (goes from 2-10). Adding jitter to the points makes it very noisy and unappealing. I like adding a alpha in the aesthetics. However, I'd like to have a legend where a reader can see how many points are overlapping for each of those transparencies. Is there such a way?

ggplot(data=mydata,aes(x=x,y=y)) + geom_point(size=3,shape=2,aes(alpha=1/3))

Let's say I use the above code. How would I incorporate a legend for the alpha?

解决方案

Here is an example of how to fake this. Try it a couple times while varying alpha.

require(ggplot2)

n = 10000
alpha = 0.01

set.seed(12345)
data = data.frame(replicate(2, rnorm(n)))

dev.new(width=4, height=3)
p = qplot(X1, X2, data=data, alpha=alpha)

fake_scale = scale_alpha('# of overlapping pts', breaks=pretty(c(alpha, 1)), labels=as.character(pretty(c(alpha, 1))/alpha))

p + fake_scale

alpha = 0.1

alpha = 0.01

这篇关于有没有办法为ggplot的alpha添加图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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