oswaldosantos / ggsn

R package to add north symbols and scale bars to maps created with ggplot or ggmap
http://oswaldosantos.github.io/ggsn/
GNU General Public License v2.0
161 stars 8 forks source link

Scalebar with facet_wrap #54

Open SUsvy opened 4 years ago

SUsvy commented 4 years ago

I'm adding a scale bar (using the ggsnpackage) to a map that is faceted. I can't quite figure out how to correctly use the facet.var variable. I currently can successfully add a scalebar that appears on all facets, but cannot restrict it to a single facet (get the undefined columns selected error). It looks like the examples are for facet_grid, and I can't quite replicate the result with facet_wrap. Any help would be appreciated...

An example of my issue:

library(ggmap)
library(ggsn)

Finalmap <- get_map(location = c(lon = -23.17, lat = 15.2), zoom = 11,  
                maptype = "hybrid", scale = 2)

data <- expand.grid(x = -23.17, y = 15.2, Factor = c("A", "B", "C"))

ggmap(Finalmap) +
    geom_point(data = data, aes(x = x, y = y)) +
    facet_wrap(~ Factor) +
    scalebar(dist = 3, dist_unit = "km",
            x.min = -23.2, x.max = -23.1, y.min = 15.1, y.max = 15.3,
            anchor = c(x = -23.12, y = 15.12),
            height = 0.04, st.dist = 0.1,
            st.color = "white", 
         transform = TRUE, model = "WGS84",
#### THESE TWO LINES ARE THE PROBLEM
         facet.var = c("Factor"), 
         facet.lev = "C")