Description
I'm having a super weird issue with ggsave
and, after sharing it at RStudio community, Mara Averick suggested I open an issue here. So here I am 😅
Basically, the legends of plots I generate using ggsave
do not match what is displayed in RStudio's "Plots" window nor the legends of plots I generate using RStudio's "Export" option. This is happening for maps created using geom_sf
.
Here's an example:
library(rnaturalearth)
library(ggplot2)
world <-
rnaturalearth::ne_countries(scale = "medium", returnclass = "sf") # creates a r object called "world" with spatial data
ggplot2::ggplot(data = world) + #starts plot with "world" as its data
geom_sf(aes(color = continent), show.legend = "point") # draws map with colored continent outlines
Created on 2020-04-10 by the reprex package (v0.3.0)
As you can see, the map above has points as its legend key. That is the output I wanted to get, however, when I use ggsave("ggsave_test.pdf")
to save the map as a pdf file this is what I get:
The weird thing is that this problem seems to be exclusive to ggsave since saving the plot using RStudio's "Export" functionality or knitting a Rmarkdown document result in the plot having points as its legend key.
This happens for .ps, .pdf, .jpeg, .tiff, .png, .bmp, and .svg files both when leaving ggsave()
's plot argument blank (e.g., when it uses last_plot()
like in the reprex above), and when using the saved map as the argument for plot. There's an exception though I used purrr::walk
to test the different file formats and the .eps file that was generated using the named map as ggsave()'s "plot" argument has the points as its key ! The plots generated using ggsave(filename = here::here("ggsave_no_loop.eps"), plot = map), and purrr::walk
without the named map as plot's argument have the squares as their keys though.
I think this might be a macOS-specific issue (or even something that is happening only in my computer) because this problem doesn't happen in rstudio.cloud.
Cheers and thanks for your amazing work!