[英]Same/Fix alpha even for overlapping areas in ggplot2
我想先畫一堆區域,然后用相同的單個 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)
我希望將其作為我的結果:
由代表 package (v2.0.1) 於 2022 年 7 月 26 日創建
我覺得我的問題的答案可能與此線程中的答案相似,但我並不完全理解,因此不確定。 另請注意,我使用geom_rect()
作為代表,但最終我希望它適用於ggforce::geom_circle()
。
Quinten 的第一個答案指向scale_alpha(range =..., limits =...)
,不幸的是沒有回答我的問題,因為它顯然只能導致不透明的區域。
Quinten 的更新答案是我可以接受的上述 reprex 的解決方法。 但是,正如我上面提到的,我也希望這適用於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)
)
由代表 package (v2.0.1) 於 2022 年 8 月 17 日創建
使用sf
和purrr
:
geom_sf
顯示結果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
)
sf_rectangle <- function(xmin,xmax,ymin,ymax) {
st_polygon(list(rbind(c(xmin, ymin), c(xmin, ymax), c(xmax, ymax), c(xmax, ymin), c(xmin, ymin))))
}
rect_union <- dat %>% purrr::pmap(sf_rectangle) %>%
purrr::reduce(st_union)
ggplot(rect_union) + geom_sf(alpha=.2)
好的,我偶然發現了{ggblend}及其blend()
function。 基本上,我需要在geom_
之后添加%>% blend("source")
,它會覆蓋多個半透明區域,它完全符合我的要求。
但是,它確實具有以下限制,可以在下面的 reprex Warning
中看到,即您實際上無法預覽 RStudio 中的 plot,甚至可能無法將其導出到某些文件類型,因為並非所有設備都可以處理這個(事情。 但是,通過device = cairo_pdf
導出到 pdf 工作正常,我在下面顯示它的屏幕截圖。
不過,我將保留這個問題,直到我找到一種同時導出.png
和.svg
的方法,或者有人提出適用於 reprex 1 和 reprex 2 的替代解決方案。
# remotes::install_github("mjskay/ggblend")
library(ggblend)
library(ggforce)
library(tidyverse)
# 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, "png", 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').
ggsave(plot = p1, "p1.pdf", device = cairo_pdf)
#> Saving 7 x 5 in image
# 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, "png", 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').
ggsave(plot = p2, "p2.pdf", device = cairo_pdf)
#> Saving 7 x 5 in image
使用reprex v2.0.2創建於 2022-08-17
也許你想要這個,你可以使用帶有range
和limits
的scale_alpha
來保持區域在相同的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")
由代表 package (v2.0.1) 於 2022 年 7 月 26 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.