有没有办法为 ggplot 的 alpha 添加图例?

发布于 2024-09-08 05:38:00 字数 247 浏览 0 评论 0原文

我有一个有许多重叠点的图(从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑丑阿 2024-09-15 05:38:00

以下是如何伪造此内容的示例。尝试几次,同时改变 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

在此处输入图像描述

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

enter image description here

alpha = 0.01

enter image description here

断舍离 2024-09-15 05:38:00

不完全是你想要的,但是 geom_hex() 怎么样?

如果你不离散化(bin)它,我认为R将需要计算重叠面积和重叠度(sp?)的数量(这也取决于点的大小),这听起来很难。

library(hexbin)

mydata <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(data=mydata,aes(x=x,y=y)) + geom_hex()

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.

library(hexbin)

mydata <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(data=mydata,aes(x=x,y=y)) + geom_hex()
一口甜 2024-09-15 05:38:00

根据其他答案的提示,您似乎可能希望将 xy 点划分为唯一值,然后将每个 xy 坐标箱的唯一值的数量相加。

使用另一个答案中的虚假数据,

library(ggplot2)
library(dplyr)
library(magrittr)

n = 10000

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

我按小数点后第一位进行分箱,然后计算每个箱中有多少个

data2 <- data %>% mutate(
  x = round(X1,1),  
  y = round(X2,1)
) %>% 
  group_by(x,y) %>%
  tally() %>% 
  dplyr::filter(n<=10) %>% 
  mutate(transp = n/10) 

箱,我仅出于说明目的过滤掉了最大的箱。现在,transp 列包含您想要的计算,您可以将其提供给 alpha 并在图例

data2 %>% 
  ggplot(aes(x=x,y=y)) + 
  geom_point(aes(alpha = as.factor(transp))) + 
  scale_alpha_discrete(
    "Number of overlapping points",
    labels = 1:10
  )+ 
  theme(legend.position = "bottom")

图片中获取它:

在此处输入图像描述

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,

library(ggplot2)
library(dplyr)
library(magrittr)

n = 10000

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

I'm binning by the 1st decimal place, and then counting how many are in each bin

data2 <- data %>% mutate(
  x = round(X1,1),  
  y = round(X2,1)
) %>% 
  group_by(x,y) %>%
  tally() %>% 
  dplyr::filter(n<=10) %>% 
  mutate(transp = n/10) 

I filtered out the biggest bins just for illustration purposes. Now, the transp column has the calculation you want, which you can supply to alpha and get it in the legend

data2 %>% 
  ggplot(aes(x=x,y=y)) + 
  geom_point(aes(alpha = as.factor(transp))) + 
  scale_alpha_discrete(
    "Number of overlapping points",
    labels = 1:10
  )+ 
  theme(legend.position = "bottom")

pic:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文