有没有办法为 ggplot 的 alpha 添加图例?
我有一个有许多重叠点的图(从 2 到 10)。向这些点添加抖动会使其变得非常嘈杂且没有吸引力。我喜欢在美学上添加一个阿尔法。但是,我想要一个图例,让读者可以看到每个透明胶片有多少点重叠。有这样的办法吗?
ggplot(data=mydata,aes(x=x,y=y)) + geom_point(size=3,shape=2,aes(alpha=1/3))
假设我使用上面的代码。我如何为 alpha 添加图例?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何伪造此内容的示例。尝试几次,同时改变
alpha
。alpha = 0.1
alpha = 0.01
Here is an example of how to fake this. Try it a couple times while varying
alpha
.alpha = 0.1
alpha = 0.01
不完全是你想要的,但是 geom_hex() 怎么样?
如果你不离散化(bin)它,我认为R将需要计算重叠面积和重叠度(sp?)的数量(这也取决于点的大小),这听起来很难。
Not exactly what you want, but how about geom_hex()?
If you don't discretize (bin) it, I think R would need to calculate the overlapped area and the number of overlappedness(sp?) (which would also depend on point size), and that sounds hard.
根据其他答案的提示,您似乎可能希望将 xy 点划分为唯一值,然后将每个 xy 坐标箱的唯一值的数量相加。
使用另一个答案中的虚假数据,
我按小数点后第一位进行分箱,然后计算每个箱中有多少个
箱,我仅出于说明目的过滤掉了最大的箱。现在,
transp
列包含您想要的计算,您可以将其提供给alpha
并在图例图片中获取它:
Taking cues from other answers, it seems like you may want to bin the x-y points into unique values, and then sum up the number of unique values per each x-y coordinate bin.
Using the fake data from another answer,
I'm binning by the 1st decimal place, and then counting how many are in each bin
I filtered out the biggest bins just for illustration purposes. Now, the
transp
column has the calculation you want, which you can supply toalpha
and get it in the legendpic: