如果你想有一个传奇,那么你必须映射到美学上,即不是设置
fill
和
pattern_fill
当参数将它们移动到内部时
aes()
。之后,您可以使用以下命令设置所需的颜色
scale_fill_manual
和
scale_pattern_fill_manual
.
使用基于默认示例的最小可重复示例
?geom_sf
:
library(ggplot2)
library(ggpattern)
county_sf <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- county_sf[c(1, 3, 5, 7), ]
m <- county_sf[c(1, 2, 3), ]
ggplot() +
geom_sf(data = county_sf, fill = NA) +
geom_sf_pattern(
data = b,
aes(pattern_fill = "b"),
pattern_colour = "black", pattern_size = 0.01
) +
geom_sf(
data = m,
aes(fill = "m"),
alpha = 0.5
) +
scale_fill_manual(
values = c("black"),
name = NULL
) +
scale_pattern_fill_manual(
values = c("red"),
name = NULL
) +
theme_void() +
theme(
legend.position = "bottom"
)