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 9 forks source link

Error when creating scalebar in faceted plot when underlying data are simple features #24

Closed aakarner closed 6 years ago

aakarner commented 6 years ago

There seems to be a problem in the scalebar() function when the underlying data are simple features and the plot is faceted.

Reproducible example:

library(ggsn); library(sf)
dsn <- system.file('extdata', package = 'ggsn')

# Map in geographic coordinates
map <- st_read(dsn, 'sp', quiet = TRUE)

(ggm1 <- ggplot(map, aes(fill = nots)) +
    geom_sf() +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8))

ggm1 +
  blank() +
  facet_grid(~ nots) + 
  north(map) +
  scalebar(map, dist = 5, dd2km = TRUE, model = 'WGS84',
           facet.var = "nots", facet.lev = "[0,10]")

Throws this error:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

It seems that this line in scalebar():

data[, facet.var[i]] <- factor(data[, facet.var[i]])

is the culprit and breaks because data[, facet.var[i]] returns a simple features data frame containing two variables, nots and geometry. It makes sense to change the right hand side to pull(data, facet.var[i]) but I'm not sure about the best way to change the underlying class to factor on the left hand side. There are also several other spots in the code where factor(data[, facet.var[i]]) should also use pull().

oswaldosantos commented 6 years ago

Check ggsn 0.4.7, please.

aakarner commented 6 years ago

That works! Thanks.