Home > Software design >  degree symbol in plot R with ggplot2 using RStudio
degree symbol in plot R with ggplot2 using RStudio

Time:12-29

I have this code and I'm using RStudio

library(tidyverse)
library(osmdata)
bbx <- getbb("hawaii")

border <- bbx %>%
  opq() %>%
  add_osm_feature(key = "place",
                  value = c("island")) %>%
  osmdata_sf()

border

ggplot()  
  geom_sf(
    data = border$osm_lines,
    inherit.aes = FALSE,
    color = "black",
    size = .4,
    alpha = .8
  )

when I plot this I have this

1

As you can see instead of the degree symbol I have a white rectangle in labels.

How can I solve this problem?

Thanks in advance

P.S.: I'm using Window11 64bit and RStudio updated with default options and all packages updated.

CodePudding user response:

The problem you are encountering with the white rectangle appears to be related to a character encoding issue, specifically with the degree symbol (°). It seems that the degree symbol is not being properly rendered in your plot.

There are a few things you can try to resolve this issue:

  1. Make sure that your system and RStudio are using the correct character encoding. In RStudio, you can check the character encoding by going to the menu "Tools > Global Options > Code > Saving", and looking at the "Default text encoding" setting. If it is set to something other than "UTF-8", you can try changing it to "UTF-8" and see if that fixes the problem.

  2. If changing the character encoding does not work, you can try using a different font that includes the degree symbol. You can do this by specifying the font argument in the geom_sf() function. For example:

ggplot() geom_sf(data = border$osm_lines,inherit.aes = FALSE,color = "black",size = .4,alpha = .8,font = "Arial")

You can try using different fonts to see if any of them work for you. Some fonts that are known to include the degree symbol and work well with ggplot2 include "Arial", "DejaVu Sans", and "Lato".

CodePudding user response:

Following @tjebo I did two tests:

  1. I change the Graphics Device option to CAIRO ( Tools -> General -> Graphics -> Backend)

and it worked

  1. I downgrade R to R4.2.1

and that worked too.

Thank you so much tjebo

  • Related