【问题标题】:R/ggplot2: Add png with transparency infoR/ggplot2:添加带有透明度信息的 png
【发布时间】:2021-12-10 09:46:28
【问题描述】:

我想在 ggplot2 图上堆叠 PNG 图像。其中一个 png 具有 alpha/透明度信息,但它被忽略了。

如何在 bg.png 顶部添加第二张图片 (image1.png),以便保留透明度并且在黄色背景上仅显示黑色文本?

示例

library(ggplot2)
library(png)
library(patchwork)

img1 <- readPNG("bg.png", native = TRUE)
img2 <- readPNG("image1.png", native = TRUE)

ggp <- ggplot(data.frame()) +
  geom_point() + 
  theme_nothing() +
  inset_element(p = img1,
                left = 0,
                bottom = 0,
                right = 1,
                top = 1,
                align_to = "full") +
  inset_element(p = img2,
                left = 0,
                bottom = 0,
                right = 0.5,
                top = 0.5,
                align_to = "full")

ggp

示例文件:

【问题讨论】:

标签: r ggplot2 png


【解决方案1】:

我认为 alpha 通道 在这里受到尊重。只不过@9​​87654323@先画了一个白色的canvas元素。可以说,您尝试做的不是插图,而是注释。

我认为在这里使用ggplot2 中的annotation_custom 更合适(并且意味着要少加载一个包)。

library(ggplot2)
library(png)

img1 <- grid::rasterGrob(readPNG("bg.png"))
img2 <- grid::rasterGrob(readPNG("image1.png"))

ggp <- ggplot(data.frame()) +
  geom_point() + 
  theme_void() +
  annotation_custom(img1) +
  annotation_custom(img2, xmax = 0.5, ymax = 0.5)

ggp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2012-08-31
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多