6

Does anyone know why the legend of the size aestatic BIR74 won't show the dot sizes but rectangles? If the answer is yes, how can I fix this?

Reproducable example:

library(sf)
# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

nc <- st_read(system.file("shape/nc.shp", package="sf"))

nc_centers <- st_centroid(nc)

nc_centers %>%
  ggplot() +
  geom_sf(aes(color = SID79, size = BIR74)) +
  coord_sf(datum = NA) +
  theme_minimal()

enter image description here

Tdebeus
  • 1,519
  • 5
  • 21
  • 43
  • No sample data means few people will be able to play with your sample code. There exists a [related and known issue](https://github.com/tidyverse/ggplot2/issues/2037) that was closed on 1 November 2017. Have you tried updating `ggplot2`? – Maurits Evers Mar 29 '18 at 12:20
  • 4
    The example HAS sample data. Thanks I'll try update it. – Tdebeus Mar 29 '18 at 12:26
  • Does the same for me with sf:0.6.1 and ggplot2:2.2.1.9000 (which is older than the bug report...) – Spacedman Mar 29 '18 at 12:27
  • 1
    Still fails with the github version via devtools. With just the `size` aesthetic you get wrong legend objects. `ggplot(nc_centers, aes(size=BIR74)) + geom_sf()` Solution? Use `tmap` instead? – Spacedman Mar 29 '18 at 12:37
  • 2
    You're right on the all accounts; I can confirm the issue. I suggest leaving a comment at the respective [`sf`](https://github.com/r-spatial/sf/issues/88) or [`ggplot2`](https://github.com/tidyverse/ggplot2/issues/2037) GitHub issues. – Maurits Evers Mar 29 '18 at 12:39
  • this is a duplicate: https://stackoverflow.com/questions/47996634/add-line-legend-to-geom-sf?rq=1 – sebdalgarno Mar 29 '18 at 16:47

1 Answers1

12

you need to add the show.legend argument to geom_sf, i.e.

nc_centers %>%
  ggplot() +
  geom_sf(aes(color = SID79, size = BIR74), show.legend = 'point') +
  coord_sf(datum = NA) +
  theme_minimal()

enter image description here

sebdalgarno
  • 2,929
  • 12
  • 28