【发布时间】:2021-02-25 22:31:43
【问题描述】:
目标
我正在尝试在 ggplot 中创建一张法国 2017 年选举结果的地图,用获胜政党的颜色填充每个选民。
问题
当我使用 geom_sf 映射法国选民的 shapefile 时,填充参数无法识别我为每个政党的颜色提供的十六进制值。相反,它会为每一方分配一种新颜色。
我的地图应该是这样的颜色 correct party colours
但是 ggplot 目前的颜色是这样的 incorrect party colours
代码
我有一个包含所有法国选民的 shapefile,here,在 R 中看起来像
df
dept_circ party_2017 dept_nom geometry
1 001_01 LR Ain POLYGON ((4.887938 46.41241...
2 001_02 LR Ain POLYGON ((4.728168 45.94598...
3 001_03 LREM Ain POLYGON ((5.570681 45.75369...
4 001_04 LREM Ain POLYGON ((4.739192 46.04677...
5 001_05 LR Ain POLYGON ((5.247852 45.94869...
6 002_01 LREM Aisne POLYGON ((3.310763 49.68271...
7 002_02 LR Aisne POLYGON ((3.055322 49.83207...
8 002_03 PS Aisne POLYGON ((3.3149 49.95589, ...
9 002_04 LREM Aisne POLYGON ((3.036076 49.3252,...
10 002_05 LREM Aisne POLYGON ((2.957951 49.22691...
我为每一方创建了一组相应的颜色(前五方如下所示)
colours_fra_parties <- c(
"PCF" = "#E4002B",
"DVG" = "#FFC0C0",
"FI" = "#C9462C",
"FI (E)"= "#C9462C",
"PS" = "#ED1651") # ... and so on for every single party
我像这样将 shapefile 和颜色输入 ggplot
df %>%
ggplot() +
geom_sf(
aes(fill = party_2017),
colour = "#000000", # Colour the border around each electorate
lwd = 0.1 # Specify width of the border lines
) +
coord_sf( # Centre the map on metropolitan France
xlim = c(-5, 10), # Degrees longitude west (-) and east (+) of GMT to map
ylim = c(41.5, 51.5)) + # Degrees latitude south (-) and north (+) of the equator
scale_colour_manual( # Feed geom_sf the correct colours of each party
values = colours_fra_parties)
但结果产生了incorrect party colours
geom_sf 无法识别我输入的十六进制颜色,我做错了什么?
【问题讨论】:
-
你试过
scale_fill_manual吗?