Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draw_image() does not position transparent images correctly #147

Closed
gauravdiwan89 opened this issue Aug 29, 2019 · 4 comments
Closed

draw_image() does not position transparent images correctly #147

gauravdiwan89 opened this issue Aug 29, 2019 · 4 comments
Milestone

Comments

@gauravdiwan89
Copy link

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
Copy link
Contributor

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
Copy link
Author

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

@clauswilke
Copy link
Contributor

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 clauswilke reopened this Sep 2, 2019
@clauswilke clauswilke added this to the cowplot 1.1 milestone Aug 21, 2020
@clauswilke
Copy link
Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants