使用 geom_sf 时如何更改图例形状?
How to change legend shapes when using geom_sf?
我正在使用 geom_sf
和 ggplot
绘制点,并且想更改点的形状。我可以在地图上更改它们,但图例从不反映点的形状,即使使用 override.aes
也是如此。
我不知道我是否遗漏了什么或者这是一个错误。我在 Tidyverse 问题跟踪器中追踪了类似的问题,this one is quite similar. But none of the "resolved" 问题似乎解决了我的问题。
这是一个示例,显示 ggplot
如何无法将形状传播到图例。
library(sf)
library(ggplot2)
cities <- tibble::tribble(
~ lon, ~ lat, ~ name, ~ pop,
5.121420, 52.09074, "Utrecht", 311367,
6.566502, 53.21938, "Groningen", 189991,
4.895168, 52.37022, "Amsterdam", 779808
) %>% sf::st_as_sf(coords = c("lon", "lat"), crs = 4326)
lines_sfc <- sf::st_sfc(list(
sf::st_linestring(rbind(cities$geometry[[1]], cities$geometry[[2]])),
sf::st_linestring(rbind(cities$geometry[[2]], cities$geometry[[3]]))
))
lines <- sf::st_sf(
id = 1:2,
size = c(10,50),
geometry = lines_sfc,
crs = 4326
)
ggplot(cities) +
geom_sf(aes(shape = name))
ggplot(cities) +
geom_sf(aes(shape = name)) +
scale_shape_manual(values = c(1:3),
guide = guide_legend(
override.aes = list(shape = c(1:3))))
我希望图例条目具有与地图相同的形状,但我得到的是空方块。
ggplot() +
geom_sf(data = cities, aes(shape = name), show.legend = "point") +
scale_shape_manual(values = c(1, 2, 3))
我正在使用 geom_sf
和 ggplot
绘制点,并且想更改点的形状。我可以在地图上更改它们,但图例从不反映点的形状,即使使用 override.aes
也是如此。
我不知道我是否遗漏了什么或者这是一个错误。我在 Tidyverse 问题跟踪器中追踪了类似的问题,this one is quite similar. But none of the "resolved" 问题似乎解决了我的问题。
这是一个示例,显示 ggplot
如何无法将形状传播到图例。
library(sf)
library(ggplot2)
cities <- tibble::tribble(
~ lon, ~ lat, ~ name, ~ pop,
5.121420, 52.09074, "Utrecht", 311367,
6.566502, 53.21938, "Groningen", 189991,
4.895168, 52.37022, "Amsterdam", 779808
) %>% sf::st_as_sf(coords = c("lon", "lat"), crs = 4326)
lines_sfc <- sf::st_sfc(list(
sf::st_linestring(rbind(cities$geometry[[1]], cities$geometry[[2]])),
sf::st_linestring(rbind(cities$geometry[[2]], cities$geometry[[3]]))
))
lines <- sf::st_sf(
id = 1:2,
size = c(10,50),
geometry = lines_sfc,
crs = 4326
)
ggplot(cities) +
geom_sf(aes(shape = name))
ggplot(cities) +
geom_sf(aes(shape = name)) +
scale_shape_manual(values = c(1:3),
guide = guide_legend(
override.aes = list(shape = c(1:3))))
我希望图例条目具有与地图相同的形状,但我得到的是空方块。
ggplot() +
geom_sf(data = cities, aes(shape = name), show.legend = "point") +
scale_shape_manual(values = c(1, 2, 3))