LastBorn
LastBorn

Reputation: 123

degree symbol in plot R with ggplot2 using RStudio

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.

Upvotes: 0

Views: 2334

Answers (4)

I have found that this works well in R version 4.2.3. (recently released in March 2023). After some unproductive struggle with other options, following the suggestion of @LastBorn, I tested with an older version I had already installed (4.1.1.) and it worked, and I saw that there is a new release that is also good. Happy plotting!

Upvotes: 0

Mukesh
Mukesh

Reputation: 21

The problem occurs at two points. First when you plot in console and second when you export using ggsave. I am providing solution for both and it is quite simple.

  1. For in console plot: Change the graphics to cairo (Tools -> General -> Graphics -> Backend > Cairo

  2. While exporting the image with ggsave use the value (type = "cairo"). Example

    ggsave("img.png", height = 18, width = 30, units = 'in', dpi= 200, type = "cairo")
    

This worked for me. Hope this helps.

Upvotes: 2

LastBorn
LastBorn

Reputation: 123

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

Upvotes: 8

MATEUS ALMEIDA
MATEUS ALMEIDA

Reputation: 13

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".

Upvotes: 0

Related Questions