-
Notifications
You must be signed in to change notification settings - Fork 2.1k
geom_sf : error when using comma as decimal separator #3365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@thomasp85 and/or @clauswilke - is the support for unicode in ggplot2/grid robust enough to just use the degree symbol rather than resort to plotmath? |
unicode support is a matter of the graphic device, not ggplot2/grid... |
To answer your question better - most devices support unicode characters, but using plotmath ensures that the symbol font is used and by that, that the degree sign glyph is present in the font... |
The expression text is generated here in sf: https://github.com/r-spatial/sf/blob/master/R/graticule.R#L220-L235 Surrounding the degree text in quotes when created seems safest. @edzer - if the |
As far as I can tell, this particular problem arises because ggplot creates a plotmath expression, then converts it into a string, and then parses the string again. At that parsing stage, the number with a comma as separator is not understood. I don't remember now why I wrote the code this way, but there was a valid reason for it. I think it's because we build an internal data frame that holds x and y labels in the same column, and it is possible that only one of the two sets of labels uses plotmath. One general solution may be to store everything as plotmath, rather than everything as strings. Turning numbers into strings may also work. Finally, if the R locale settings are changed so it understands a comma as a separator (I assume this is possible, I never use it) things should also work. |
Relevant sections in the code: Lines 80 to 83 in b560662
Lines 116 to 123 in b560662
|
A workaround for now is to either set the labels manually or to provide functions that generate labels that R can parse later on. library(ggplot2)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.6/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
degreeLabelsNS <- function(x) {
pos = sign(x) + 2
dir = c("*S", "", "*N")
ggplot2:::parse_safe(paste0('"', abs(x), '"', "*degree", dir[pos]))
}
degreeLabelsEW <- function(x) {
x <- ifelse(x > 180, x - 360, x)
pos = sign(x) + 2
if (any(x == -180))
pos[x == -180] = 2
if (any(x == 180))
pos[x == 180] = 2
dir = c("*W", "", "*E")
ggplot2:::parse_safe(paste0('"', abs(x), '"', "*degree", dir[pos]))
}
options(OutDec = ",")
ggplot() +
geom_sf(data = nc) +
scale_x_continuous(
labels = degreeLabelsEW
) +
scale_y_continuous(
labels = degreeLabelsNS
) Created on 2019-06-17 by the reprex package (v0.3.0) |
It might make sense to add these helper functions to the scales package, which holds all sorts of similar functions, and to make them more configurable (number of decimal places shown, etc.). We can't change the source code of sf every time we want the labels to look a little different. |
Just for completeness, here is the variation with a unicode degree symbol. If we provide these helper functions in scales, they could take an option to either generate plotmath or strings with unicode. library(ggplot2)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.6/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
degreeLabelsNS <- function(x) {
pos = sign(x) + 2
dir = c("S", "", "N")
paste0(abs(x), "°", dir[pos])
}
degreeLabelsEW <- function(x) {
x <- ifelse(x > 180, x - 360, x)
pos = sign(x) + 2
if (any(x == -180))
pos[x == -180] = 2
if (any(x == 180))
pos[x == 180] = 2
dir = c("W", "", "E")
paste0(abs(x), "°", dir[pos])
}
options(OutDec = ",")
ggplot() +
geom_sf(data = nc) +
scale_x_continuous(
labels = degreeLabelsEW
) +
scale_y_continuous(
labels = degreeLabelsNS
) Created on 2019-06-17 by the reprex package (v0.3.0) |
The advantage of improving these functions in |
@edzer My main point was that in the general case, we want to have these functions exported to the user and configurable. We may want to specify number of decimals, whether or not there's a space between the number and the degree symbol (as in |
Regardless of the home for alternative graticule calculations, I think it's reasonable to have In ggplot2, it causes these expectations to fail: ggplot2/tests/testthat/test-coord_sf.R Lines 160 to 173 in b560662
I think it won't be hard to find another way to test this. |
Thanks for this fast evaluation and workaround. |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Hi.
There is an error when plotting a map of a
sf
object if the decimal separator is set to the comma in R options.traceback
As a workaround we can revert to the dot decimal separator before the plot...
session info
``` sessionInfo() #> R version 3.6.0 (2019-04-26) #> 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=French_France.1252 LC_CTYPE=French_France.1252 #> [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C #> [5] LC_TIME=French_France.1252 #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages: #> [1] sf_0.7-4 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.1 #> [5] purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_2.1.2 #> [9] ggplot2_3.1.1 tidyverse_1.2.1 #> #> loaded via a namespace (and not attached): #> [1] tidyselect_0.2.5 xfun_0.7 haven_2.1.0 #> [4] lattice_0.20-38 colorspace_1.4-1 generics_0.0.2 #> [7] htmltools_0.3.6 yaml_2.2.0 rlang_0.3.4 #> [10] e1071_1.7-1 pillar_1.4.1 glue_1.3.1 #> [13] withr_2.1.2 DBI_1.0.0 modelr_0.1.4 #> [16] readxl_1.3.1 plyr_1.8.4 munsell_0.5.0 #> [19] gtable_0.3.0 cellranger_1.1.0 rvest_0.3.4 #> [22] evaluate_0.14 knitr_1.23 class_7.3-15 #> [25] highr_0.8 broom_0.5.2 Rcpp_1.0.1 #> [28] KernSmooth_2.23-15 scales_1.0.0 backports_1.1.4 #> [31] classInt_0.3-3 jsonlite_1.6 hms_0.4.2 #> [34] digest_0.6.19 stringi_1.4.3 grid_3.6.0 #> [37] cli_1.1.0 tools_3.6.0 magrittr_1.5 #> [40] lazyeval_0.2.2 crayon_1.3.4 pkgconfig_2.0.2 #> [43] xml2_1.2.0 lubridate_1.7.4 assertthat_0.2.1 #> [46] rmarkdown_1.13 httr_1.4.0 R6_2.4.0 #> [49] units_0.6-3 nlme_3.1-139 compiler_3.6.0 ```The text was updated successfully, but these errors were encountered: