thomasp85 / ggforce

Accelerating ggplot2
https://ggforce.data-imaginist.com
Other
917 stars 105 forks source link

facet_zoom with mapping #57

Open jonocarroll opened 7 years ago

jonocarroll commented 7 years ago

Similar but different to #56. With geom_sf() on its way to ggplot2 it would be great to have projection-aware facet_zooming. Part of this would likely require that the original and zoomed panels not necessarily be the same height/width, in order to maintain projections. For example, it would be great to be able to zoom a region of a map

library(dplyr)
library(maps)
library(ggplot2) 
library(ggforce) 
aus_map <- map_data("world") %>% 
  filter(region == "Australia", 
         is.na(subregion) | subregion %in% c("Tasmania", "Kangaroo Island"))
p <- ggplot() + 
  geom_polygon(aes(x = long, y = lat, group = group), 
               colour = NA, 
               data = aus_map) 
p + facet_zoom(xlim = c(131, 148), ylim = c(-45, -32), 
               zoom.size = 4)

Attempting to do similar with the sf branch of ggplot2 just produces the unzoomed map in both panels and seems to miss the zoom highlight, though it certainly looks neater.

library(sf)      
library(maps)
library(ggplot2) # devtools::install_github("tidyverse/ggplot2@sf") ## NOTE sf branch
library(ggforce)
world1 <- sf::st_as_sf(map('world', plot = FALSE, fill = TRUE))
ggplot() + 
  geom_sf(data = world1) + 
  coord_sf(xlim = c(110, 160), ylim = c(-45, -10)) + 
  facet_zoom(xlim = c(132, 148), ylim = c(-45, -32))

This has potential to look really awesome if the unzoomed panel could have its height adjusted to maintain projection (width can currently be somewhat manually adjusted via zoom.size) and if the subsetting was compatible with the sf features (appropriate highlight/zoom).

I know it's a dev feature, so this is just 'down the track' thinking.

johnmutiso commented 5 years ago

You won't be able to specify axis limits twice..., therefore, the xlim and ylim in the coord_sf function affects the functionality of facet_zoom

jonocarroll commented 5 years ago

I haven't looked at this for a while, but that makes sense - is there a better way to do the zoom, or does this need something like inherit.aes = FALSE?