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

strange interaction of geom_raster and scale_{x,y}_continuous #3539

Closed
paciorek opened this issue Sep 23, 2019 · 4 comments · Fixed by #3547
Closed

strange interaction of geom_raster and scale_{x,y}_continuous #3539

paciorek opened this issue Sep 23, 2019 · 4 comments · Fixed by #3547

Comments

@paciorek
Copy link

@paciorek paciorek commented Sep 23, 2019

If one applies scale_{x,y}_continuous to a raster plot but sets the x- or y-axis limits to be smaller than the extent of the data grid, the resulting plotting of the raster cells is distorted, with the cells shrunk in the dimension where the limit is set "too" small.

## Setup simple grid.
dat = expand.grid(x = seq(-2, 2, by = 1), y = seq(-2, 2, by = 1))
dat$val <- (1:nrow(dat))/nrow(dat)

## Case A: This plots fine without scale_{x,y}_continuous.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0)

## Case B: This plots fine too with limits set to be larger than extent of data grid.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0) + scale_x_continuous(limits = c(-3.5, 3.5)) + scale_y_continuous(limits = c(-3.5,3.5))

## Case C: y limits  are now more restricted than data limits and don't align with the data grid.
## Scaling in y direction is now distorted.
## Note that the center of the middle grid box should be at (0,0) where the horizontal and vertical lines intersect.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0) + scale_x_continuous(limits = c(-2.5, 2.5)) + scale_y_continuous(limits = c(-2,2.5))

Here's info on my setup:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

[...snip ...]

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] ggplot2_3.2.1    
@clauswilke
Copy link
Member

@clauswilke clauswilke commented Sep 23, 2019

Could you please run your example through the reprex package and repost here? https://reprex.tidyverse.org/articles/articles/magic-reprex.html

@paciorek
Copy link
Author

@paciorek paciorek commented Sep 23, 2019

Here it is.

library(ggplot2)

## Setup simple grid.
dat = expand.grid(x = seq(-2, 2, by = 1), y = seq(-2, 2, by = 1))
dat$val <- (1:nrow(dat))/nrow(dat)

## Case A: This plots fine without scale_{x,y}_continuous.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0)

## Case B: This plots fine too with limits set to be larger than extent of data grid.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0) + scale_x_continuous(limits = c(-3.5, 3.5)) + scale_y_continuous(limits = c(-3.5,3.5))

## Case C: y limits  are now more restricted than data limits and don't align with the data grid.
## Scaling in y direction is now distorted.
## Note that the center of the middle grid box should be at (0,0) where the horizontal and vertical lines intersect.
ggplot() + geom_raster(data = dat, aes(x = x, y = y, fill = val)) + geom_hline(yintercept = 0) + geom_vline(xintercept = 0) + scale_x_continuous(limits = c(-2.5, 2.5)) + scale_y_continuous(limits = c(-2,2.5))

Created on 2019-09-23 by the reprex package (v0.3.0)

thomasp85 added a commit to thomasp85/ggplot2 that referenced this issue Oct 1, 2019
@thomasp85
Copy link
Member

@thomasp85 thomasp85 commented Oct 1, 2019

This was a bug - thanks for reporting. Just an FYI, you'd probably want to zoom using the xlim and ylim arguments to coord_cartesian() rather than using the scales. The latter will completely remove data that falls outside of the scale limits while the former will just zoom in as expected

@lock
Copy link

@lock lock bot commented Jun 24, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants