R: add alpha-value to png-image

前端 未结 1 420
刺人心
刺人心 2020-12-17 19:07

Is there a way to make a rasterGrob-object partly transparent, so to add an alpha-factor to it? I\'m using a logo as a watermark within a ggplot2 plot by insert

1条回答
  •  独厮守ぢ
    2020-12-17 19:47

    Straight from the ggplot2 blog by @baptiste. You can adjust alpha when you create w.

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

    enter image description here

    Or if you want to have a single image:

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

    enter image description here

    0 讨论(0)
提交回复
热议问题