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

scalebar() data arg secretly requires column names that start with 'lat' and 'long' #46

Closed DesiQuintans closed 5 years ago

DesiQuintans commented 5 years ago

In ggsn v0.5.0, the scalebar() function will fail if the names of the columns containing latitude and longitude data do not start with lat and long, respectively:

library(ggmap)
library(ggsn)

coords <- 
    data.frame(stringsAsFactors = FALSE,
               x = c(150.750034, 150.6857303, 150.533333, 150.416667, 150.279,
                     150.3730633, 150.5625494, 150.65),
               y = c(-33.613769, -33.5801886, -33.5, -33.55, -33.514, -33.7094058,
                     -33.7002999, -33.666667))

head(coords)

#>          x         y
#> 1 150.7500 -33.61377
#> 2 150.6857 -33.58019
#> 3 150.5333 -33.50000
#> 4 150.4167 -33.55000
#> 5 150.2790 -33.51400
#> 6 150.3731 -33.70941

my_map <- qmplot(x, y, data = coords, geom = "blank", 
                 maptype = "terrain-background", zoom = 11)

my_map + 
    scalebar(data = coords, dist = 5, dist_unit = "km", model = "WGS84", 
             transform = TRUE)

#> Warning in min(data$long): no non-missing arguments to min; returning Inf
#> Warning in max(data$long): no non-missing arguments to max; returning -Inf
#> Warning in min(data$lat): no non-missing arguments to min; returning Inf
#> Warning in max(data$lat): no non-missing arguments to max; returning -Inf
#> Warning in sin(lat): NaNs produced
#> Warning in cos(phi): NaNs produced
#> Warning in sin(phi): NaNs produced

#> Warning in sin(phi): NaNs produced
#> Warning in sin(lat): NaNs produced
#> Warning in cos(phi): NaNs produced
#> Warning in sin(phi): NaNs produced

#> Warning in sin(phi): NaNs produced
#> `data` must be uniquely named but has duplicate columns

And no map will be generated. Compare this to the magic names:

names(coords) <- c("long", "lat")

my_map + 
    scalebar(data = coords, dist = 5, dist_unit = "km", model = "WGS84", 
             transform = TRUE)