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

Inverted raster images #63

Closed
debruine opened this issue Nov 16, 2020 · 9 comments
Closed

Inverted raster images #63

debruine opened this issue Nov 16, 2020 · 9 comments

Comments

@debruine
Copy link

When displaying the background image in a ggplot using annotation_custom() and a rasterGrob, the image is inverted if the y-axis is reversed, but only when ragg is the graphics device. This doesn't happen with other graphics devices like quartz_png, which show the image upright. So the same code can produce different graphs depending on the render environment (e.g., knitr vs pkgdown).

See https://debruine.github.io/imginv/articles/test.html for a demonstration of the problem in situ.

This should reproduce the problem.

library(ggplot2) # also uses functions from ragg and grid

# set up raster image and plot
grad <- matrix(hcl(seq(0, 280, 40), 80, 80), nrow=8, ncol=5)
img <- grid::rasterGrob(grad)
width <- 500
height <- 800

p <- ggplot() +
  scale_y_reverse() + # this is the problem
  coord_fixed(xlim = c(0, width),
              ylim = c(0, height)) +
  annotation_custom(img, 0, width, -height, 0) +
  geom_point(aes(x, y), data.frame(x = 0, y = 0))

# displays red on top - correct
# if your computer is different from mine, this might need to have another graphics device set
# but I don't know how to do that outside knitr options, quartz_png shows the image upright
p 

# displays purple on top
file <- tempfile(fileext='.png')
ragg::agg_png(file, width = width, height = height, res = 72)
p
dev.off()
browseURL(file)
@thomasp85
Copy link
Member

thomasp85 commented Nov 19, 2020

This is the file I get when rendering to png with your code (the last part, using ragg)... Is this not what is expected?

file6f14542d217f

@thomasp85
Copy link
Member

thomasp85 commented Nov 19, 2020

It is the same output as both quartz and Cairo on my computer (I know this is borderline "works on my computer" comment, but it's just for reference :-)

@debruine
Copy link
Author

debruine commented Nov 19, 2020 via email

@thomasp85
Copy link
Member

Ah - I was on the dev version of ggplot2... (FYI, setting the limits in the coord may end up affecting the direction of the scale in future versions)

@thomasp85
Copy link
Member

I can reproduce now, though it is somewhat inconclusive...

ragg:
file731be0ee903

quartz (default macOS):
file731b74e3cb2a

cairo:
file731b3933da2c

I would say that it is actually a bug in Quartz, not in ragg and cairo.

@thomasp85
Copy link
Member

However, you may be right that you wouldn't expect scale_y_reverse() to flip the raster - that would be a ggplot2 issue I think

@thomasp85
Copy link
Member

Do you have access to a windows machine? What is the behaviour of the default windows device in case you can test?

@debruine
Copy link
Author

Thanks for looking into this. Seems I lucked into the one device that did what I want. I don't have easy access to a windows machine, sorry.

The context is plotting face images with a pre-existing associated coordinate system that starts in the upper left. I guess I'll just switch the plotting function to reverse the y coordinates, since I don't normally include the axes.

image

@thomasp85
Copy link
Member

Yeah, I agree that the behaviour is unwanted, but it should be fixed in ggplot2, not in the device

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