Open caijun opened 7 years ago
Please, update to ggsn 0.4.4.
Installing ggsn v0.4.4 solves the above error. Thanks.
When I am trying to the most recent ggsn to add scalebar to my Japan prefectures map, however, it adds nothing.
My script for downloading Japan shapefiles and plotting the map is pasted here.
remove(list = ls(all.names = TRUE))
# download Japan shapefile
temp <- tempfile()
download.file("http://tonytsai.name/materials/shp/Japan.zip", temp)
unzip(temp, exdir = "shp/")
unlink(temp)
library(sf)
japan.polygon <- read_sf("shp/Japan.shp")
japan.inline <- read_sf("shp/Japan_polyline_inline.shp")
# longitude and latitude of lower left point
ll <- st_point(c(123, 24))
# longitude and latitude of upper right point
ur <- st_point(c(148, 45))
# WGS84
sfc <- st_sfc(ll, ur, crs = 4326)
# EPSG 2460 projection
sfc_proj <- st_transform(sfc, crs = 2460)
sfc_proj_bbox <- st_bbox(sfc_proj)
library(ggsn)
p <- ggplot(japan.polygon) +
geom_sf(aes(fill = region), color = NA) +
geom_sf(data = japan.inline, color = "white", size = 0.2) +
scalebar(japan.polygon, dist = 5, dd2km = TRUE, model = 'GRS80') +
coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
print(p)
The resulting map is as follow
As you can see, no scalebar is added on the map.
My session information is as follow
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin16.7.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggsn_0.4.4 ggplot2_2.2.1.9000 sf_0.5-4
loaded via a namespace (and not attached):
[1] Rcpp_0.12.12 compiler_3.4.1 plyr_1.8.4 bindr_0.1 tools_3.4.1
[6] digest_0.6.12 tibble_1.3.4 gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1
[11] png_0.1-7 rlang_0.1.2 DBI_0.7 mapproj_1.2-5 yaml_2.1.14
[16] proto_1.0.0 bindrcpp_0.2 stringr_1.2.0 dplyr_0.7.3 RgoogleMaps_1.4.1
[21] maps_3.2.0 glue_1.1.1 R6_2.2.2 jpeg_0.1-8 foreign_0.8-69
[26] sp_1.2-5 ggmap_2.6.1 reshape2_1.4.2 udunits2_0.13 magrittr_1.5
[31] scales_0.5.0.9000 units_0.4-6 maptools_0.9-2 assertthat_0.2.0 geosphere_1.5-5
[36] colorspace_1.3-2 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 rjson_0.2.15
The CRS 2460 is in meters, so you must to set dd2km = FALSE
too short. Furthermore, 5 km seems too short.
japan.polygon <- read_sf("shp/Japan.shp")
japan.inline <- read_sf("shp/Japan_polyline_inline.shp")
japan.polygon <- st_transform(japan.polygon, crs = 2460)
japan.inline <- st_transform(japan.inline, crs = 2460)
ggplot(japan.polygon) +
geom_sf(aes(fill = region), color = NA) +
geom_sf(data = japan.inline, color = "white", size = 0.2) +
scalebar(japan.polygon, dist = 500, dd2km = F, model = 'GRS80')
The code snippet you gave works, but I also want to specify the x, y limits provided by coord_sf()
.
I changed the value of dist
and dd2km
parameters in scalebar()
function as you suggested.
p <- ggplot(japan.polygon) +
geom_sf(aes(fill = region), color = NA) +
geom_sf(data = japan.inline, color = "white", size = 0.2) +
scalebar(japan.polygon, dist = 500, dd2km = FALSE, model = 'GRS80') +
coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
print(p)
However, it still didn't add any scalebar to the map.
Try:
anchor <- c(sfc_proj_bbox[c(3, 2)])
names(anchor) <- c("x", "y")
ggplot() +
geom_sf(data = japan.polygon, aes(fill = region), color = NA) +
geom_sf(data = japan.inline, color = "white", size = 0.2) +
scalebar(japan.polygon, dist = 500, dd2km = F, model = 'GRS80',
anchor = anchor) +
coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
I will try to simply this for future versions.
The scalebar is added to the map now, however, it is reduced into a "scaleline" shown in following figure.
I have tried different values of the height
parameter to increase the scalebar height, but it seems that the height
parameter doesn't take into effect.
I tested the coed in ubunut and mac, and in both cases the following images resulted (the last with height = .1
)
It's weird. My scalebar is still a line. My session information is as follows
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin16.7.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggsn_0.4.4 ggplot2_2.2.1.9000 sf_0.5-4
loaded via a namespace (and not attached):
[1] Rcpp_0.12.12 compiler_3.4.1 plyr_1.8.4 bindr_0.1 tools_3.4.1
[6] digest_0.6.12 tibble_1.3.4 gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1
[11] png_0.1-7 rlang_0.1.2 DBI_0.7 mapproj_1.2-5 yaml_2.1.14
[16] proto_1.0.0 bindrcpp_0.2 stringr_1.2.0 dplyr_0.7.3 RgoogleMaps_1.4.1
[21] maps_3.2.0 glue_1.1.1 R6_2.2.2 jpeg_0.1-8 foreign_0.8-69
[26] sp_1.2-5 ggmap_2.6.1 reshape2_1.4.2 udunits2_0.13 magrittr_1.5
[31] scales_0.5.0.9000 units_0.4-6 maptools_0.9-2 assertthat_0.2.0 geosphere_1.5-5
[36] colorspace_1.3-2 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 rjson_0.2.15
What's yours?
What happens with the following code:
anchor <- c(x = 7e5, y = 6e5)
ggplot(japan.polygon) +
geom_sf(aes(fill = region), color = NA) +
geom_sf(data = japan.inline, color = "white", size = 0.2) +
scalebar(japan.polygon, dist = 500, dd2km = F, model = 'GRS80',
anchor = anchor) +
coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
Following is the figure output on my macOS.
And if you omit coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
?
Did you solve the problem? Another issue #23 was solved just reinstalling the packages ggmap, ggplot and sf.
No, the scalebar is still a line.
After omitting coord_sf(xlim = sfc_proj_bbox[c(1, 3)], ylim = sfc_proj_bbox[c(2, 4)], crs = 2460)
, the output figure becomes more terrible.
My session info is as follows
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin17.0.0 (64-bit)
Running under: macOS High Sierra 10.13.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggsn_0.4.6 ggplot2_2.2.1.9000 sf_0.5-5
loaded via a namespace (and not attached):
[1] Rcpp_0.12.13 bindr_0.1 compiler_3.4.2 plyr_1.8.4 class_7.3-14
[6] tools_3.4.2 digest_0.6.12 tibble_1.3.4 gtable_0.2.0 lattice_0.20-35
[11] pkgconfig_2.0.1 png_0.1-7 rlang_0.1.4 DBI_0.7 mapproj_1.2-5
[16] yaml_2.1.14 proto_1.0.0 bindrcpp_0.2 e1071_1.6-8 stringr_1.2.0
[21] dplyr_0.7.4 RgoogleMaps_1.4.1 maps_3.2.0 classInt_0.1-24 glue_1.2.0
[26] R6_2.2.2 jpeg_0.1-8 foreign_0.8-69 sp_1.2-5 ggmap_2.6.1
[31] reshape2_1.4.2 udunits2_0.13 magrittr_1.5 scales_0.5.0.9000 units_0.4-6
[36] maptools_0.9-2 assertthat_0.2.0 geosphere_1.5-7 colorspace_1.3-2 stringi_1.1.5
[41] lazyeval_0.2.1 munsell_0.4.3 rjson_0.2.15
Did you reinstall the ggmap, ggplot and sf?
Yes, I install the development versions of those packages using following commands
devtools::install_github("dkahle/ggmap")
devtools::install_github("tidyverse/ggplot2")
devtools::install_github("r-spatial/sf")
devtools::install_github("oswaldosantos/ggsn")
My session info now becomes
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin17.0.0 (64-bit)
Running under: macOS High Sierra 10.13.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggsn_0.4.6 ggplot2_2.2.1.9000 sf_0.5-6
loaded via a namespace (and not attached):
[1] Rcpp_0.12.13 bindr_0.1 compiler_3.4.2 git2r_0.19.0 plyr_1.8.4
[6] bitops_1.0-6 class_7.3-14 tools_3.4.2 digest_0.6.12 memoise_1.1.0
[11] tibble_1.3.4 gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1 png_0.1-7
[16] rlang_0.1.4 DBI_0.7 mapproj_1.2-5 curl_3.0 yaml_2.1.14
[21] proto_1.0.0 bindrcpp_0.2 e1071_1.6-8 dplyr_0.7.4 stringr_1.2.0
[26] withr_2.1.0.9000 httr_1.3.1 knitr_1.17 maps_3.2.0 RgoogleMaps_1.4.1
[31] devtools_1.13.4 classInt_0.1-24 glue_1.2.0 R6_2.2.2 jpeg_0.1-8
[36] foreign_0.8-69 sp_1.2-5 ggmap_2.7 reshape2_1.4.2 udunits2_0.13
[41] magrittr_1.5 maptools_0.9-2 scales_0.5.0.9000 units_0.4-6 assertthat_0.2.0
[46] colorspace_1.3-2 geosphere_1.5-7 stringi_1.1.5 lazyeval_0.2.1 munsell_0.4.3
[51] rjson_0.2.15
But the output figure is still the same as the above one.
I can reproduce the problematic behavior on my Windows machine and am also trying to include a scalebar while specifying the plot's extent using coord_sf()
.
My session info:
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] sf_0.5-5 ggsn_0.4.7 ggplot2_2.2.1.9000
I fixed this error by installing with install_github("oswaldosantos/ggsn")
.
I'm experiencing similar behaviour. The coordinates in my simple features are in UTM. If I don't set the crs (through coord_sf(datum='crs') ) ggplot will display correctly in decimal degrees. If I then add a north arrow I get the warning messages 'Warning messages: 1: In max(data$long) : no non-missing arguments to max; returning -Inf'. No x and y-axis coordinates will be shown. If I add a scale bar I get similar warnings but the plot will show x and y axes in decimal degrees (and just as 'x' and 'y' like caijun above). In both cases no scale bar or north arrow is shown. If I add the coord_sf the result is the same both times. When I try to add a scale bar through Ewen Gallic's method (http://egallic.fr/en/scale-bar-and-north-arrow-on-a-ggplot2-map/ ) I'm getting a very tall and narrow map, whether I use coord_sf or not and whether or not I specify lat and long in utm or decimal degree coordinates. It seems the longitude is ok, but the latitude where the scale bar is shown (in my case) is 80S instead of 14.6S. I think the same issue is related to both here, but I can't quite figure out what it is.
I just re-installed and updated R, and installed ggsn through github too.
Please, post a reproducible example to try to figure out if there is something particular with your code.
Got it to work by explicitly calling ggsn::scalebar and having ggsn 4.11.0 installed via github.
I ended up with the following:
north(data=roads_cropped, symbol=3, location="topright", scale=.1) +
ggsn::scalebar(data=roads_cropped, #using layer with largest extent
location="bottomleft", #location of scale bar
dist = 10, #distance in km to represent each segment
dd2km = FALSE, #if TRUE assumes decimal degrees, we work in UTM
height = .01, #indicate height of scale bar proportional to y-axis
dist_unit="km", #set to km
st.size=3, #set scale bar text size
st.dist=0.02, #sets distance between scale bar and text
st.bottom= FALSE) + #if true displays scale bar text at bottom of scale bar
With 'ggsn::scalebar' it throws an error message. A bit confusing, you call the scalebar function in ggsn with 'scalebar', but when you type ?scalebar to access help it shows as 'Scale bar'. 'scalebar' is also a function in 'raster'. People working with spatial data will probably often have the package raster loaded too.
The help page title is 'scalebar' now. You will definitely need explicit calls to avoid masking by loaded namesake functions.
Possible solution for others, since this is the primary landing page for this error, relates to if statements within a ggplot plot call. Format should be:
ggplot() +
{if (cond) expr} +
Alternative reason: not setting extents
with layer_spatial
I am trying to reproduce the first example of adding north and scalebar on my macOS,
but it gives following error
My session information is as follows