gpt4 book ai didi

r - 即使对于 ggplot2 中的重叠区域,相同/修复 alpha

转载 作者:行者123 更新时间:2023-12-05 01:03:58 25 4
gpt4 key购买 nike

我想先绘制一堆区域,然后用相同的单个 alpha 值显示生成的整体区域。所以不要这样:

library(tidyverse)

dat <- tribble(
~xmin, ~xmax, ~ymin, ~ymax,
10, 30, 10, 30,
20, 40, 20, 40,
15, 35, 15, 25,
10, 15, 35, 40
)

ggplot() +
geom_rect(data = dat,
aes(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax
),
alpha = 0.5)

我希望将其作为我的结果:

reprex package 于 2022-07-26 创建(v2.0.1)

我觉得我的问题的答案可能与 this thread 中的答案相似但我并不完全理解它,因此不确定。另请注意,我使用 geom_rect() 作为代表,但最终我希望它适用于 ggforce::geom_circle() .

编辑 1

Quinten's first answer ,它指向 scale_alpha(range = ..., limits = ...),不幸的是没有回答我的问题,因为它显然只能导致不透明的区域。

编辑 2

Quinten's updated answer是我可以接受上述代表的一种解决方法。但是,正如我上面提到的,我希望它适用于 ggforce::geom_circle() .不幸的是,我想我现在必须更具体并创建另一个代表。 (对不起)

library(ggforce)
#> Lade nötiges Paket: ggplot2

dat <- data.frame(
x = c(1, 1.3, 1.6),
y = c(1, 1, 1),
circle = c("yes", "yes", "no")
)

ggplot() +
coord_equal() +
theme_classic() +
geom_circle(
data = subset(dat, circle == "yes"),
aes(x0 = x, y0 = y, r = 0.5, alpha = circle),
fill = "grey",
color = NA,
show.legend = TRUE
) +
geom_point(
data = dat,
aes(x, y, color = circle)
) +
scale_color_manual(
values = c("yes" = "blue", "no" = "red")
) +
scale_alpha_manual(
values = c("yes" = 0.25, "no" = 0)
)

reprex package 于 2022-08-17 创建(v2.0.1)

最佳答案

ggblend

不久前我还看到了 ggblend,但不确定它是否能解决您的问题,幸运的是它可以解决!你可以做两件事:

  1. 您可以像这样将 Rstudio 中的 Graphics 更改为“Cairo”:

enter image description here

代码:

#remotes::install_github("mjskay/ggblend")
library(ggblend)
library(ggforce)

# reprex 1 ----------------------------------------------------------------
library(tidyverse)

dat <- tribble(
~xmin, ~xmax, ~ymin, ~ymax,
10, 30, 10, 30,
20, 40, 20, 40,
15, 35, 15, 25,
10, 15, 35, 40
)

p1 <- ggplot() +
geom_rect(data = dat,
aes(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax
),
alpha = 0.3) %>% blend("source")

p1

enter image description here

# reprex 2 ----------------------------------------------------------------
dat <- data.frame(
x = c(1, 1.3, 1.6),
y = c(1, 1, 1),
circle = c("yes", "yes", "no")
)

p2 <- ggplot() +
coord_equal() +
theme_classic() +
geom_circle(
data = subset(dat, circle == "yes"),
aes(x0 = x, y0 = y, r = 0.5, alpha = circle),
fill = "grey",
color = NA,
show.legend = TRUE
) %>% blend("source") +
geom_point(
data = dat,
aes(x, y, color = circle)
) +
scale_color_manual(
values = c("yes" = "blue", "no" = "red")
) +
scale_alpha_manual(
values = c("yes" = 0.25, "no" = 0)
)

p2
#ggsave(plot = p2, "p2.pdf", device = cairo_pdf)

enter image description here

  1. 您可以使用 type = "cairo" 将对象保存为 png,如下所示:
library(ggblend)
library(ggforce)

# reprex 1 ----------------------------------------------------------------
library(tidyverse)

dat <- tribble(
~xmin, ~xmax, ~ymin, ~ymax,
10, 30, 10, 30,
20, 40, 20, 40,
15, 35, 15, 25,
10, 15, 35, 40
)

p1 <- ggplot() +
geom_rect(data = dat,
aes(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax
),
alpha = 0.3) %>% blend("source")
#> Warning: Your graphics device, "quartz_off_screen", reports that blend = "source" is not supported.
#> - If the blending output IS NOT as expected (e.g. geoms are not being
#> drawn), then you must switch to a graphics device that supports
#> blending, like png(type = "cairo"), svg(), or cairo_pdf().
#> - If the blending output IS as expected despite this warning, this is
#> likely a bug *in the graphics device*. Unfortunately, several
#> graphics do not correctly report their capabilities. You may wish to
#> a report a bug to the authors of the graphics device. In the mean
#> time, you can disable this warning via options(ggblend.check_blend =
#> FALSE).
#> - For more information, see the Supported Devices section of
#> help('blend').

png(filename = "plot1.png", type = "cairo")
# Output in your own folder:

p1
dev.off()

enter image description here2

#ggsave(plot = p1, "p1.pdf", device = cairo_pdf)

# reprex 2 ----------------------------------------------------------------
dat <- data.frame(
x = c(1, 1.3, 1.6),
y = c(1, 1, 1),
circle = c("yes", "yes", "no")
)

p2 <- ggplot() +
coord_equal() +
theme_classic() +
geom_circle(
data = subset(dat, circle == "yes"),
aes(x0 = x, y0 = y, r = 0.5, alpha = circle),
fill = "grey",
color = NA,
show.legend = TRUE
) %>% blend("source") +
geom_point(
data = dat,
aes(x, y, color = circle)
) +
scale_color_manual(
values = c("yes" = "blue", "no" = "red")
) +
scale_alpha_manual(
values = c("yes" = 0.25, "no" = 0)
)
#> Warning: Your graphics device, "quartz_off_screen", reports that blend = "source" is not supported.
#> - If the blending output IS NOT as expected (e.g. geoms are not being
#> drawn), then you must switch to a graphics device that supports
#> blending, like png(type = "cairo"), svg(), or cairo_pdf().
#> - If the blending output IS as expected despite this warning, this is
#> likely a bug *in the graphics device*. Unfortunately, several
#> graphics do not correctly report their capabilities. You may wish to
#> a report a bug to the authors of the graphics device. In the mean
#> time, you can disable this warning via options(ggblend.check_blend =
#> FALSE).
#> - For more information, see the Supported Devices section of
#> help('blend').

png(filename = "plot2.png", type = "cairo")

# Output in your folder
p2

enter image description here

dev.off()
#ggsave(plot = p2, "p2.pdf", device = cairo_pdf)

创建于 2022-08-17,使用 reprex v2.0.2

更新

您可以做的是使用 sf 包中的 st_union 对区域进行 UNION,这样您就可以得到一个区域,而不是像这样的重叠区域:

library(tidyverse)
library(sf)

dat <- tribble(
~xmin, ~xmax, ~ymin, ~ymax,
10, 30, 10, 30,
20, 40, 20, 40,
15, 35, 15, 25,
10, 15, 35, 40
)

area1 <- dat %>%
slice(1) %>%
as_vector() %>%
st_bbox() %>%
st_as_sfc()

area2 <- dat %>%
slice(2) %>%
as_vector() %>%
st_bbox() %>%
st_as_sfc()

area3 <- dat %>%
slice(3) %>%
as_vector() %>%
st_bbox() %>%
st_as_sfc()

area4 <- dat %>%
slice(4) %>%
as_vector() %>%
st_bbox() %>%
st_as_sfc()

all_areas <- st_union(area1, area2) %>%
st_union(area3) %>%
st_union(area4)

ggplot(all_areas) +
geom_sf(alpha = 0.5, fill = "grey", colour = "grey") +
theme(legend.position = "none")

ggplot(all_areas) +
geom_sf(alpha = 0.8, fill = "grey", colour = "grey") +
theme(legend.position = "none")

reprex package 于 2022-08-17 创建(v2.0.1)

第一个答案

也许你想要这个,你可以使用 scale_alpharangelimits 来保持相同的区域 alpha 像这样:

library(tidyverse)

dat <- tribble(
~xmin, ~xmax, ~ymin, ~ymax,
10, 30, 10, 30,
20, 40, 20, 40,
15, 35, 15, 25,
10, 15, 35, 40
)

ggplot() +
geom_rect(data = dat,
aes(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax,
alpha = 0.5
)) +
scale_alpha(range = c(0, 1), limits = c(0, 0.5)) +
theme(legend.position = "none")

reprex package 于 2022-07-26 创建(v2.0.1)

关于r - 即使对于 ggplot2 中的重叠区域,相同/修复 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73119065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com