7

If I create maps using geom_sf, the axis labels have the wrong symbol for degrees. I get degree symbols that are vertically centred in the text, rather than raised like superscipts.

For example,

library(sf)                                            
library(ggplot2)                                       
nc = st_read(system.file("shape/nc.shp", package="sf"))

ggplot() +                                             
  geom_sf(data = nc) +
  theme(axis.text = element_text(size=16))

enter image description here

When I see examples online, they typically look correct (e.g. image below, copied from here), so I guess it is related to something in my local setup.

enter image description here

I have tried changing font, using library(extrafont) but this problem remains in every font I tried.

Update

I don't think this is a ggplot-specific issue, as I get the same thing with any graphics that uses the degree keyword in plotmath. For example

par(mar=c(0,0,0,0))
plot.new()
text(0.5,0.5, bquote(120*degree*N), cex=5)

enter image description here

I am on Linux (Kubuntu 19.04), R 3.5.2, ggplot2 v. 3.2.1, sf v. 0.7-7. Not sure what other information might be relevant, but I can update answer with anything else that is requested.

Community
  • 1
  • 1
dww
  • 30,425
  • 5
  • 68
  • 111

3 Answers3

9

I ran into a similar problem and after reading here and some other related issues I found a solution. I'm working with Rstudio, so I guess that if you are doing R without it you you will easily find your work around (hopefully).

My solution was to Tools -> Global Options and here I changed my backend to Cairo.

This is how it looks the change:

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
5

Finally managed to track down the answer:

From ?X11 it says:

Problems with incorrect rendering of symbols (e.g., of quote(pi) and expression(10^degree)) have been seen on Linux systems which have the Wine symbol font installed – fontconfig then prefers this and misinterprets its encoding. Adding the following lines to ‘~/.fonts.conf’ or ‘/etc/fonts/local.conf’ may circumvent this problem by preferring the URW Type 1 symbol font.

<fontconfig>
<match target="pattern">
  <test name="family"><string>Symbol</string></test>
  <edit name="family" mode="prepend" binding="same">
    <string>Standard Symbols L</string>
  </edit>
</match>
</fontconfig>

Adding these lines to /etc/fonts/local.conf solved the problem for me.

dww
  • 30,425
  • 5
  • 68
  • 111
  • legend, this fixed it. FWIW you might have to create your own etc/local/fonts.conf then reboot. I raised this issue here: https://gitlab.freedesktop.org/fontconfig/fontconfig/issues/209 – dez93_2000 Dec 09 '19 at 18:53
  • I have a similar issue, not solved by this fix. I thought it was different enough to be posted as a separate SO issue. Any help of course welcome, but leaving it here also in case it is relevant to others finding this https://stackoverflow.com/questions/60656445/how-to-fix-degree-symbol-not-showing-correctly-in-maps-made-with-ggplot2-geom-sf – giocomai Mar 13 '20 at 09:34
3

As a workaround I am creating the axis labels manually. Still looking for a better solution and explanation of the undesired behaviour.

xlabs = seq(-84,-76, 2)
ylabs = seq(34, 36.5, 0.5)
ggplot() +                                             
  geom_sf(data = nc) +
  scale_x_continuous(breaks = xlabs, labels = paste0(xlabs,'°W')) +
  scale_y_continuous(breaks = ylabs, labels = paste0(ylabs,'°N')) +
  theme(axis.text = element_text(size=16))

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111