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 trying to add scale bar #22

Closed PaulLantos closed 6 years ago

PaulLantos commented 7 years ago

Hi Oswaldo,

I'm trying to add a scale bar to a ggplot object that includes several layers. There is an sf object (read from a shapefile using st_read( ) ) that I want to use to produce the scale bar. Here is my code and the error:

ggplot( prediction ) + theme_bw() + geom_raster( aes( Longitude , Latitude , fill = Probability )) + scale_fill_viridis( option = "B" , limits = c( 0, 0.6 )) + geom_sf(data=counties, color="gray5", fill="NA", cex=0.1) + geom_contour( aes( x=Longitude , y=Latitude , z=p) , breaks = c( 0.025 ), color="Blue" , cex=1.2, lty=1) + geom_contour( aes( x=Longitude , y=Latitude , z=p) , breaks = c( 0.975 ), color="Red" , cex=1.2, lty=1) + geom_sf(data=buffer, color="black", fill="NA", cex=2) + scalebar(data=buffer, dist = 25, dd2km = TRUE, x.min=-79.83613, x.max=-78.04790, y.min=35.11765, y.max=36.89916, model = "WGS84")

Here's the error: Warning messages: 1: In max(data$long) : no non-missing arguments to max; returning -Inf 2: In min(data$lat) : no non-missing arguments to min; returning Inf 3: In max(data$lat) : no non-missing arguments to max; returning -Inf 4: In min(data$lat) : no non-missing arguments to min; returning Inf 5: In max(data$lat) : no non-missing arguments to max; returning -Inf 6: In min(data$lat) : no non-missing arguments to min; returning Inf

The map plots fine otherwise, but with no scale bar image

oswaldosantos commented 7 years ago

Send a reproducible example please

PaulLantos commented 7 years ago

The data are in the excel file prediction.xlsx, and the two shapefiles are in the zip file. For the ggplot code I pasted to work just read counties.shp into object counties and buffer.shp into buffer.

prediction.xlsx shapefiles.zip

PaulLantos commented 7 years ago

I get the same error if I just use a single shapefile alone (pick, for instance, counties.shp in the above zip file).

`counties<-st_read("counties.shp")

ggplot(counties) + geom_sf() + scalebar(data=counties, dist = 5, dd2km = TRUE, model = "WGS84")`

ggplot(counties) + geom_sf() + scalebar(data=counties, dist = 5, dd2km = TRUE, model = "WGS84") There were 15 warnings (use warnings() to see them) warnings() Warning messages: 1: In max(data$long) : no non-missing arguments to max; returning -Inf 2: In min(data$lat) : no non-missing arguments to min; returning Inf 3: In max(data$lat) : no non-missing arguments to max; returning -Inf 4: In min(data$lat) : no non-missing arguments to min; returning Inf 5: In max(data$lat) : no non-missing arguments to max; returning -Inf 6: In min(data$lat) : no non-missing arguments to min; returning Inf 7: In sin(lat) : NaNs produced 8: In cos(phi) : NaNs produced 9: In sin(phi) : NaNs produced 10: In sin(phi) : NaNs produced 11: In sin(lat) : NaNs produced 12: In cos(phi) : NaNs produced 13: In sin(phi) : NaNs produced 14: In sin(phi) : NaNs produced 15: Removed 2 rows containing missing values (geom_text)

oswaldosantos commented 7 years ago

What's your session?

R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.04

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

other attached packages:
[1] sf_0.5-4           ggsn_0.4.4         ggplot2_2.2.1.9000

map

PaulLantos commented 7 years ago

sessionInfo() R version 3.4.1 (2017-06-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

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

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

other attached packages: [1] ggsn_0.4.0 sf_0.5-4 rstan_2.16.2
[4] StanHeaders_2.16.0-1 brms_1.9.0 Rcpp_0.12.12
[7] mgcv_1.8-19 nlme_3.1-131 dplyr_0.7.2
[10] purrr_0.2.3 readr_1.1.1 tidyr_0.7.1
[13] tibble_1.3.4 ggplot2_2.2.1.9000 tidyverse_1.1.1
[16] RevoUtilsMath_10.0.0

oswaldosantos commented 7 years ago

Check your code with:

[1] sf_0.5-4           ggsn_0.4.4         ggplot2_2.2.1.9000
PaulLantos commented 7 years ago

Ok, will do. Thanks for the help!