【问题标题】:Inserting a transparent logo to the ggplot2 background将透明徽标插入 ggplot2 背景
【发布时间】:2021-10-31 13:53:59
【问题描述】:

我想在 ggplot 背景中添加一个透明图像\logo。

我尝试了以下方法。

 m <- readPNG(".\\Input\\Logo.png", FALSE)

 w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1])

 qplot(1:10, rnorm(10), geom = "blank") +
 annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,    
 rasterGrob(w)) + geom_point()

但我收到一个错误:m[, , 4] 中的错误:下标超出范围。

感谢您对此的支持。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我没有.\\Input\\Logo.png 但是,试试这个:

    library(png)
    m <- readPNG(system.file("img", "Rlogo.png", package="png"))
    w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1])
    qplot(1:10, rnorm(10), geom = "blank") +
      annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,    
                        rasterGrob(w)) + geom_point()
    

    【讨论】:

    • 我在使用 Rlogo.png 时没有收到错误消息,但在使用我需要的徽标时却收到了错误消息。图像尺寸为 223 * 92 像素。是不是因为尺寸标志
    • 也许你有一个 RGB 类型的 PNG(3 个颜色通道)而不是 RGBA 类型(3 个颜色通道 + 1 个 alpha 通道)?提供真实图像来创建可重现的示例将是一个好主意,你不觉得吗? :->
    • 试试 w &lt;- abind::abind(m, matrix(0.2, ncol = ncol(m[,,3]), nrow = nrow(m[,,3]))) - 0.2 是您的 alpha/透明度值。可能有一种更优雅的方法可以将第四维添加到您的图像数组中......但我想这会产生另一个问题。
    • 我无法加载 PNG 图像,因此无法在此处发布。
    【解决方案2】:

    根据@lukeA的comment w &lt;- abind::abind(m, matrix(0.2, ncol = ncol(m[,,3]), nrow = nrow(m[,,3]))) - 0.2 为我工作。

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2016-01-22
      • 2011-03-08
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多