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() fails: In min(x) : no non-missing arguments to min; returning Inf #50

Closed marcos-lee closed 5 years ago

marcos-lee commented 5 years ago
library(ggmap)
library(ggplot2)
library(ggsn)

register_google(key = APIKEY)

map_saosebast <- ggmap(get_googlemap("sao sebastiao, sp, brazil",
                                     scale = 2, zoom = 12,
                                     maptype ='terrain',
                                     color = 'color'))

x_lim <- map_saosebast$data[c(1, 4), 1] * c(1, .9998)
y_lim <- map_saosebast$data[c(1, 4), 2] * c(1.003, 1)

map_saosebast + scalebar(x.min = x_lim[1], x.max = x_lim[2],
                        y.min = y_lim[1], y.max = y_lim[2],
                        dist = 1, dist_unit = 'km', transform = TRUE, model = 'WGS84')

Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: Removed 3 rows containing missing values (geom_text). 

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggsn_0.5.3    ggmap_3.0.0   ggplot2_3.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2         pillar_1.4.2       compiler_3.6.1     plyr_1.8.4         class_7.3-15       bitops_1.0-6      
 [7] tools_3.6.1        digest_0.6.20      jsonlite_1.6       tibble_2.1.3       gtable_0.3.0       lattice_0.20-38   
[13] pkgconfig_2.0.2    png_0.1-7          rlang_0.4.0        DBI_1.0.0          rstudioapi_0.10    curl_4.0          
[19] e1071_1.7-2        withr_2.1.2        dplyr_0.8.3        httr_1.4.1         stringr_1.4.0      RgoogleMaps_1.4.4 
[25] classInt_0.4-1     tidyselect_0.2.5   glue_1.3.1         sf_0.7-7           R6_2.4.0           jpeg_0.1-8        
[31] foreign_0.8-71     sp_1.3-1           purrr_0.3.2        tidyr_0.8.3        magrittr_1.5       units_0.6-4       
[37] scales_1.0.0       maptools_0.9-5     assertthat_0.2.1   colorspace_1.4-1   labeling_0.3       KernSmooth_2.23-15
[43] stringi_1.4.3      lazyeval_0.2.2     munsell_0.5.0      crayon_1.3.4       rjson_0.2.20 

Map plots, but no scalebar. Using the values by hand in x.min, etc, produces the same error.

oswaldosantos commented 5 years ago

Define x_lim and y_lim within the bounding box.

x_lim <- map_saosebast$data[c(1, 4), 1] * c(.9998, 1.003)
y_lim <- map_saosebast$data[c(1, 4), 2] * c(.9998, 1.003)
marcos-lee commented 5 years ago

Thank you, this works.

However, the bounding box values seems to change according to the amount of zoom I use in the get_googlemap function, e.g.:

map_saosebast <- ggmap(get_googlemap("sao sebastiao, sp, brazil",
                                     scale = 2, zoom = 13,
                                     maptype ='terrain',
                                     color = 'color'))

x_lim <- map_saosebast$data[c(1, 4), 1] * c(.9998, 1.003)
y_lim <- map_saosebast$data[c(1, 4), 2] * c(.9998, 1.003)

map_saosebast + scalebar(x.min = x_lim[1], x.max = x_lim[2],
                         y.min = y_lim[1], y.max = y_lim[2],
                         dist = 1, dist_unit = 'km', transform = TRUE, model = 'WGS84')

Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: Removed 2 rows containing missing values (geom_text). 

In the example above, it fails, but if I change the bounding box multiple to x_lim <- map_saosebast$data[c(1, 4), 1] * c(.99998, 1.0003) then it works.

Is there maybe a more systematic way of doing this?

oswaldosantos commented 5 years ago

Maybe but I have no thought in that so far. If you find one let me know.