使用ggplot定义alpha来突出显示单点
可重复的代码:
library(ggplot2)
library(data.table)
x = rnorm(10000, 0, 1)
y = rnorm(10000, 0, 1)
dat = data.table(x,y)
dat[,id := as.character(1:.N)]
ALPHA = 0.2
dat[,alpha := ifelse(id == 42, 1, ALPHA)]
alpha2 = unique(dat[,list(id, alpha)])
alpha = alpha2[,alpha]
names(alpha) = dat[,id]
u = ggplot(dat, aes(x, y, alpha = id)) + geom_point()
u = u + scale_alpha_manual(values = alpha, guide = FALSE)
u
输出:
ID = 42的点未突出显示,并且连续alpha-values仍然适用于所有观察结果。我只想看到ID = 42的点,其alpha为1,其余的所有固定值为alpha = 0.2。
Reproducible code:
library(ggplot2)
library(data.table)
x = rnorm(10000, 0, 1)
y = rnorm(10000, 0, 1)
dat = data.table(x,y)
dat[,id := as.character(1:.N)]
ALPHA = 0.2
dat[,alpha := ifelse(id == 42, 1, ALPHA)]
alpha2 = unique(dat[,list(id, alpha)])
alpha = alpha2[,alpha]
names(alpha) = dat[,id]
u = ggplot(dat, aes(x, y, alpha = id)) + geom_point()
u = u + scale_alpha_manual(values = alpha, guide = FALSE)
u
Output:
Issue:
The point with id = 42 is not highlighted, and continuous alpha-values are still applied to all observations. I would like to see only the point with id = 42 having an alpha of 1 and all the rest with a fixed value of alpha = 0.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一个选择是使用注释。由于使用alpha = 0.2时的可见性不是很好,因此我将其染色(但也可以与Alpha参数一起使用)。
使用alpha:
Another option is to use annotate. Since the visibility is not very good when using alpha = 0.2, , I colored it red (but would work with an alpha argument too).
Using alpha:
alpha 美学上映射
alpha
列并使用Alpha
美学来实现所需结果您可以通过在
100,因为否则几乎不可能看到这种方法有效。
注2:使用不透明度的恕我直言并不是突出一分的最佳方法,尤其是在大量点上。
You could achieve your desired result by mapping your
alpha
column on thealpha
aesthetic and usingscale_alpha_identity
:Note 1: I set the number of points to 100 because otherwise it's nearly impossible to see that this approach works.
Note 2: IMHO using opacity is not the best way to highlight one point, especially with a large number of points.