Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Allow different resolutions on different facets for geom_raster and/or geom_tile #2103
Comments
|
Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you! If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down the page. |
karawoo
added the
reprex
label
Jun 13, 2017
MagicForrest
commented
Jun 14, 2017
|
Hi Karawoo-Cruise! many thanks for picking this up, and sorry not to provide a reprex before, here it is now. You will see that there is a different problem with both geom_raster() and geom_tile() when plotting different resolutions in different panels. However, I think both can be fixed by re-running the code that determines the size of the boxes (either centre,height,width or xmin,xmax,ymin,ymax) separately for the data for each panel after the facetting has been requested. As I mentioned before, I think this is done by the resolution() function. library(ggplot2)
# set up two data frames to hold spatial data at different resolutions
DF1 <- data.frame(x=rep(seq(0, 2, 0.25), 9),
y=rep(seq(0, 2, 0.25), each = 9),
v=rnorm(81), w="a")
DF2 <- data.frame(x=rep(seq(0, 2, 0.5), 5),
y=rep(seq(0, 2, 0.5), each = 5),
v=rnorm(25), w="b")
# Plotting each of the above indivdually works fine with both geom_tile and geom_raster
# DF1 with geom_tile
p <- ggplot(DF1, aes(x,y,fill=v)) + geom_tile()
print(p)# DF1 with geom_raster
p <- ggplot(DF1, aes(x,y,fill=v)) + geom_raster()
print(p)# DF2 with geom_tile
p <- ggplot(DF2, aes(x,y,fill=v)) + geom_tile()
print(p)# DF2 with geom_raster
p <- ggplot(DF2, aes(x,y,fill=v)) + geom_raster()
print(p)# combine the data frame to plot with facet_wrap
DF <- rbind(DF1, DF2)
# geom_tile reduces the data to a common resolution that fits both datasets
p <- ggplot(DF, aes(x,y,fill=v)) + geom_tile()
p <- p + facet_wrap(~w, ncol=1)
print(p)# geom_raster tries to keep the resolution of each, but doesn't get it quite right
# (notice that in panel b the boxes are no longer correctly centered on 0.0, 0.5, 1.5 and 2.0)
p <- ggplot(DF, aes(x,y,fill=v)) + geom_raster()
p <- p + facet_wrap(~w, ncol=1)
print(p) |






MagicForrest commentedApr 12, 2017
The geom_tile and geom_raster functions are fantastic for plotting maps. However, it is not uncommon to want to compare maps with different resolutions on the same plot, ie as different facets. Whilst it is easy to build a data.frame containing the relevant data and structure, it seems that the geom_tile and geom_raster functions do not handle this correctly. They try valiantly, but don't quite manage it...
There is half a workaround, using interpolate does fill in the white spaces, but it comes out so blurry that it hurts my eye to look at it. It is definitely not suitable for a report, publication etc...
As far as I can see, it might be possible to correctly handle different resolutions in different panels. One would need to include the recalculation of ymin, ymax, xmin and xmax, (or equivalent) in the facetting command, perhaps following a fresh call to resolution() for each facet (perhaps at the finish_data stage). The details are highly speculative, I just looked at the ggplot2 source code for the first time today.
Could this be feasible? It would be a really great for many geo-scientists and statisticians!