使用ggplot定义alpha来突出显示单点

发布于 2025-01-21 12:03:10 字数 778 浏览 0 评论 0原文

可重复的代码:

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:

output of code

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 技术交流群。

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

发布评论

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

评论(2

旧夏天 2025-01-28 12:03:10

另一个选择是使用注释。由于使用alpha = 0.2时的可见性不是很好,因此我将其染色(但也可以与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)]

alphapoint <- 42

ggplot(dat, aes(x, y)) + 
  geom_point(alpha = 0.2) +
  annotate("point",
           dat$x[alphapoint],
           dat$y[alphapoint],
           color = "red",
           size = 4)

使用alpha:

ggplot(dat, aes(x, y)) + 
  geom_point(alpha = 0.05) +
  annotate("point",
           dat$x[alphapoint],
           dat$y[alphapoint],
           alpha = 1)

”在此处输入图像描述

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).

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)]

alphapoint <- 42

ggplot(dat, aes(x, y)) + 
  geom_point(alpha = 0.2) +
  annotate("point",
           dat$x[alphapoint],
           dat$y[alphapoint],
           color = "red",
           size = 4)

enter image description here

Using alpha:

ggplot(dat, aes(x, y)) + 
  geom_point(alpha = 0.05) +
  annotate("point",
           dat$x[alphapoint],
           dat$y[alphapoint],
           alpha = 1)

enter image description here

终难愈 2025-01-28 12:03:10

alpha 美学上映射alpha列并使用Alpha美学来实现所需结果

您可以通过在100,因为否则几乎不可能看到这种方法有效。

注2:使用不透明度的恕我直言并不是突出一分的最佳方法,尤其是在大量点上。

library(ggplot2)
library(data.table)

set.seed(1)

x = rnorm(100, 0, 1)
y = rnorm(100, 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] 

ggplot(dat, aes(x, y, alpha = alpha)) + 
  geom_point() + 
  scale_alpha_identity(guide = FALSE)
#> Warning: It is deprecated to specify `guide = FALSE` to remove a guide. Please
#> use `guide = "none"` instead.

“”

You could achieve your desired result by mapping your alpha column on the alpha aesthetic and using scale_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.

library(ggplot2)
library(data.table)

set.seed(1)

x = rnorm(100, 0, 1)
y = rnorm(100, 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] 

ggplot(dat, aes(x, y, alpha = alpha)) + 
  geom_point() + 
  scale_alpha_identity(guide = FALSE)
#> Warning: It is deprecated to specify `guide = FALSE` to remove a guide. Please
#> use `guide = "none"` instead.

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