簡體   English   中英

即使對於 ggplot2 中的重疊區域,相同/修復 alpha

[英]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()

編輯 1

Quinten 的第一個答案指向scale_alpha(range =..., limits =...) ,不幸的是沒有回答我的問題,因為它顯然只能導致不透明的區域。

編輯 2

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 日創建

使用sfpurrr

  • 創建多邊形列表(矩形)
  • 創建一個多面體作為這些矩形的聯合
  • 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

在此處輸入圖像描述

也許你想要這個,你可以使用帶有rangelimitsscale_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM