wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

draw_image() does not position transparent images correctly #147

Closed gauravdiwan89 closed 4 years ago

gauravdiwan89 commented 5 years ago

I am trying to draw a few transparent images over my ggplot. I wish to position the transparent image at the very bottom left of the plot. I used x = 0 and y = 0 to position this transparent image. However draw_image() somehow does not recognise transparency and instead positions the bottom left corner of the entire image at those co-ordinates (first plot below).

To try and improve this, I tried the image_trim function of the magick package. However, even here the left-most part (in this case the blue dice) is not quite at x = 0 (second plot below). I understand there is an hjust option, but I am running this operation for hundreds of images and cannot define the hjust in every case.

Is there a way of getting the true left most edge of a transparent image to x = 0?

Thank you

library(cowplot,quietly = T)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************
library(ggplot2,quietly = T)

##basic version
ggplot(data.frame(x = 0:3, y = 0:3), aes(x, y)) +
  geom_point(size = 3) + 
  draw_image("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png",x=0,y=0)


##trimmed version
img <- magick::image_read("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")
img <- magick::image_trim(img)

ggplot(data.frame(x = 0:3, y = 0:3), aes(x, y)) +
  geom_point(size = 3) + 
  draw_image(img,x=0,y=0)

Created on 2019-08-29 by the reprex package (v0.3.0)

clauswilke commented 5 years ago

The problem is not the transparency, it's the aspect ratio of the image and how that interacts with the way images are placed. There is no easy fix, and a proper fix would require an entirely different placement API. Maybe look into the ggimage package and see if that better fits your needs.

gauravdiwan89 commented 5 years ago

Thank you for the suggestion. ggimage is indeed more useful (even though a little finicky).

clauswilke commented 5 years ago

Let's keep the issue open. After some reflection, I think a fix is needed, and I'll look into it when I can.

clauswilke commented 4 years ago

This is now possible in the development version, via additional paramters halign and valign (which in most cases should have the same values as hjust and vjust).

library(ggplot2)
library(cowplot)

img <- magick::image_read("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")
img <- magick::image_trim(img)

ggplot(data.frame(x = 0:3, y = 0:3), aes(x, y)) +
  geom_point(size = 3) + 
  draw_image(img, x=0, y=0, valign = 0, halign = 0)

Created on 2020-08-21 by the reprex package (v0.3.0)