Repository navigation
Closed
Description
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.
library(tidyverse)
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# works fine
ggplot() +
geom_sf(data = nc)
# raises an error
options(OutDec = ",")
ggplot() +
geom_sf(data = nc)
#>Error in parse(text = text[[i]]) : <text>:1:3: unexpected ','
#>1: 34,
^traceback
traceback()
#>15: parse(text = text[[i]])
#>14: parse_safe(graticule$degree_label[needs_parsing])
#>13: f(..., self = self)
#>12: self$fixup_graticule_labels(graticule, scale_x, scale_y, params)
#>11: f(..., self = self)
#>10: self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
#>9: (function (scale_x, scale_y)
#> {
#> self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
#> })(dots[[1L]][[1L]], dots[[2L]][[1L]])
#>8: mapply(FUN = f, ..., SIMPLIFY = FALSE)
#>7: Map(setup_panel_params, scales_x, scales_y)
#>6: f(..., self = self)
#>5: layout$setup_panel_params()
#>4: ggplot_build.ggplot(x)
#>3: ggplot_build(x)
#>2: print.ggplot(x)
#>1: (function (x, ...)
#> UseMethod("print"))(x)As a workaround we can revert to the dot decimal separator before the plot...
Activity
paleolimbot commentedon Jun 17, 2019
@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?
thomasp85 commentedon Jun 17, 2019
unicode support is a matter of the graphic device, not ggplot2/grid...
thomasp85 commentedon Jun 17, 2019
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...
paleolimbot commentedon Jun 17, 2019
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
degree_labels in ast_graticule()look like"0.4"*degree*Winstead of0.4*degree*W, would that cause problems elsewhere?clauswilke commentedon Jun 17, 2019
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.
clauswilke commentedon Jun 17, 2019
Relevant sections in the code:
ggplot2/R/coord-sf.R
Lines 80 to 83 in b560662
ggplot2/R/coord-sf.R
Lines 116 to 123 in b560662
clauswilke commentedon Jun 17, 2019
A workaround for now is to either set the labels manually or to provide functions that generate labels that R can parse later on.
Created on 2019-06-17 by the reprex package (v0.3.0)
clauswilke commentedon Jun 17, 2019
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.
clauswilke commentedon Jun 17, 2019
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.
Created on 2019-06-17 by the reprex package (v0.3.0)
edzer commentedon Jun 17, 2019
The advantage of improving these functions in
sfwould be that they'd also work for base plots, we get less duplication. @paleolimbot I'm fine if there is no further fallout from this change (haven't tried).clauswilke commentedon Jun 17, 2019
@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
35 °N), etc. Seedollar_format()anddollar()as examples: https://rdrr.io/cran/scales/man/dollar_format.html It fits into scales, but of course you could also add similar functions to sf.paleolimbot commentedon Jun 17, 2019
Regardless of the home for alternative graticule calculations, I think it's reasonable to have
coord_sf()defaults work regardless of locale. I'll let @edzer have a look at the PR to sf to see if this this will cause problem elsewhere.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.
dicorynia commentedon Jun 18, 2019
Thanks for this fast evaluation and workaround.
update sf label tests (#3378, fixes #3365)
update sf label tests (#3378, fixes #3365)
update sf label tests (#3378, fixes #3365) (#3472)
lock commentedon Dec 28, 2019
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/