ggimage icon indicating copy to clipboard operation
ggimage copied to clipboard

geom_image(): flip and rotate an image

Open moldach opened this issue 6 years ago • 4 comments

Is it possible to rotate/flip/fop an image with geom_image()?

I would like to get the mirror-image based on a binary variable. For example, given that z is either "A" or "B" have points which are "B" be the mirror-image.

library("ggplot2")
library("ggimage")

set.seed(2017-02-21)
d <- data.frame(x = rnorm(10),
                y = rnorm(10),
                z = sample(c("A","B")),
                image = sample(c("https://www.r-project.org/logo/Rlogo.png", "https://jeroenooms.github.io/images/frink.png"), size=10, replace = TRUE))

moldach avatar May 23 '18 02:05 moldach

currently, you can use something like this:

ggplot(subset(d, z=="A"), aes(x,y, image=image)) + 
    geom_image(angle=45) + 
    geom_image(angle=-45, data=subset(d, z=="B"))

GuangchuangYu avatar May 23 '18 04:05 GuangchuangYu

The images used to have a background that matches the plot (grey) but when you apply your suggested work-around to rotate the images there is now white background visible which is ugly. I tried setting theme_bw() which kind of works; however, you still see a white background box surrounding fink and the Rlogo which overlays the plot background.

Guessing, it's currently not possible to get a mirror-image (like image_flop() from the magick package) according to a subset?

moldach avatar May 24 '18 16:05 moldach

try:

ggplot(subset(d, z=="A"), aes(x,y, image=image)) + 
  geom_image(image_fun=image_flop) +
  geom_image(data=subset(d, z=="B"))

GuangchuangYu avatar May 25 '18 01:05 GuangchuangYu

Has angle been deprecated? What's the suggested approach for rotating images with geom_image?

AndyBunn avatar May 29 '20 19:05 AndyBunn