oswaldosantos/ggsn

scalebar() fails ungracefully if 'dist' is too large for the map

DesiQuintans opened this issue · 0 comments

In ggsn v0.5.0, the scalebar() function does not warn the user if the dist argument is too large for the map. This results in part of the scalebar being cut off (if dist goes off the edge of the plot area on only one edge) or the entire bar just going missing if dist is large enough that both sides go outside the plot area:

library(ggmap)
library(ggsn)

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

head(coords)

#>       long       lat
#> 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(long, lat, data = coords, geom = "blank", 
                 maptype = "terrain-background", zoom = 11)

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

This is the extent of this map, about 44 km wide.

my_map + 
    scalebar(data = coords, dist = 25, dist_unit = "km", model = "WGS84", 
             transform = TRUE, location = "bottomleft")
#> Warning: Removed 1 rows containing missing values (geom_text).

The right half of the scale bar cannot fit in the plot area.

my_map + 
    scalebar(data = coords, dist = 25, dist_unit = "km", model = "WGS84", 
             transform = TRUE, location = "bottomright")
#> Warning: Removed 1 rows containing missing values (geom_text).

The left half cannot fit.

my_map + 
    scalebar(data = coords, dist = 2500, dist_unit = "km", model = "WGS84", 
             transform = TRUE, location = "bottomright")
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Warning: Removed 2 rows containing missing values (geom_text).

This is what happens when neither half of the scale bar can fit.

my_map + 
    scalebar(data = coords, dist = 1, dist_unit = "m", model = "WGS84", 
             transform = TRUE, location = "bottomright")

And the above too-large example looks very much like this too-small example.