0

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.

tjebo
  • 21,977
  • 7
  • 58
  • 94
LastBorn
  • 123
  • 8
  • 1
    check if the answers in https://stackoverflow.com/questions/60656445/how-to-fix-degree-symbol-not-showing-correctly-in-r-on-linux-fedora-31 helps – tjebo Dec 28 '22 at 14:10
  • 1
    also possibly related https://stackoverflow.com/questions/57467046/degree-symbol-incorrect-in-map-axis-labels – tjebo Dec 28 '22 at 14:10
  • 1
    here a related GitHub issue https://github.com/r-lib/ragg/issues/51 - although this is specifically with a ragg device. Have you tried using a different device? e.g., Cairo ? – tjebo Dec 28 '22 at 14:12

4 Answers4

8

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

LastBorn
  • 123
  • 8
  • please consider turning this answer into a comment, upvoting the answer that led to success and marking the question as closed (if not already done) – I_O Dec 28 '22 at 17:36
  • 1
    @I_O this is an answer and it refers to my comment to the OP's question. You may also consider upvoting this as it gives a useful solution to the problem. LastBorn, you may consider accepting your own answer and thus marking the question as answered. – tjebo Jan 07 '23 at 09:32
2

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.

Mukesh
  • 21
  • 3
0

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

0

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!