英文:
Add custom legend in ggplot map
问题
我在下面的撒丁岛地图上添加自定义图例时遇到了困难。这个图例应该包括颜色线条(NUTS2和NUTS3边界)和代表我进行大气模拟的起始点的颜色点。
以下是使用ggplot2构建撒丁岛地图的代码:
# 获取意大利的NUTS2和NUTS3地区
nuts2_italy <- gisco_get_nuts(year="2021",nuts_level = 2,country = "Italy")
sardinia_nuts2 <- nuts2_italy[nuts2_italy$NUTS_NAME=='Sardegna',]
nuts3_italy <- gisco_get_nuts(year="2021",nuts_level = 3,country = "Italy")
# 加载起始点地图
start_pt <- st_read("data/HYSPLIT_start_points/start_points.shp")
# 绘制撒丁岛的起始点和NUTS边界
install.packages("ggspatial")
library(ggspatial)
map <- ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
))+
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif",color="#887e6a" )+
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
axis.text=element_text(family = "serif",colour = "#887e6a"))
我得到了这张地图:
我尝试在ggplot地图中添加"legend",代码如下:
map <- ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
))+
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif",color="#887e6a" )+
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
axis.text=element_text(family = "serif",colour = "#887e6a")) +
legend('topright', cex=1.2, legend=c('NUTS2 border', 'NUTS3 borders','study grid', 'Initiation points'),
lty = c(1, 1, 2, NA), # tell are which objects to be drawn as a line in the legend
pch=c(NA, NA, NA, 21), # tell are which objects to be drawn as a symbol
col=c('black', 'black', 'black', 'black'), # color of the legend
pt.bg=c('blue', "#887e6a", "#887e6a", "red"), # color of the symbol
bty='n') #turn off the legend BORDER
map
实际上,这并不起作用,我得到了错误:Error: (converted from warning) Duplicated aesthetics after name standardisation: fontface
我的意图是获得一个图例,其中包括:
- 1条蓝色实线作为"NUTS2边界"
- 1条棕色实线作为"NUTS3边界"
- 1条虚线作为"study grid"
- 1个红色点作为"initiation points"
有什么建议可以帮助我吗?谢谢!
英文:
I face difficulties to add a custom legend on the Sardinia Island map below. This legend should include both color lines (NUTS2 and NUTS3 borders) and color points which represent the initiation points from where I performed atmospehir simulations.
Here is the code for the Sardinia map construction with ggplot2:
# gets NUTS2 & NUTS3 regions of Italy
nuts2_italy <- gisco_get_nuts(year="2021",nuts_level = 2,country = "Italy")
sardinia_nuts2 <- nuts2_italy[nuts2_italy$NUTS_NAME=='Sardegna',]
nuts3_italy <- gisco_get_nuts(year="2021",nuts_level = 3,country = "Italy")
# load initiation point map
start_pt<- st_read("data/HYSPLIT_start_points/start_points.shp")
# map starting points and NUTS boundaries of Sardinia
install.packages("ggspatial")
library(ggspatial)
map<-ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
))+
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif",color="#887e6a" )+
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
axis.text=element_text(family = "serif",colour = "#887e6a"))
I obtain this map
I tried to add "legend" in my ggplot map as this:
map<-ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
))+
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif",color="#887e6a" )+
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
axis.text=element_text(family = "serif",colour = "#887e6a")) +
legend('topright', cex=1.2, legend=c('NUTS2 border', 'NUTS3 borders','study grid', 'Initiation points'),
lty = c(1, 1, 2, NA), # tell are which objects to be drawn as a line in the legend
pch=c(NA, NA, NA, 21), # tell are which objects to be drawn as a symbol
col=c('black', 'black', 'black', 'black'), # color of the legend
pt.bg=c('blue', "#887e6a", "#887e6a", "red"), # color of the symbol
bty='n') #turn off the legend BORDER
map
Actually this doesn't work and I obtained: Error: (converted from warning) Duplicated aesthetics after name standardisation: fontface
The intention is to obtain a legend with
- 1 blue full line as "NUTS2 border"
- 1 brown full line as "NUTS3 border"
- 1 dashed black line as "study grid"
- 1 red point as "initiation points"
Any idea to help me?
Thanks!
答案1
得分: 1
这是完整的代码和地图图例,感谢@stefan的帮助:
map <- ggplot() +
geom_sf(data=nuts3_italy, aes(color = "nuts3" ), fill = "antiquewhite", show.legend = "line") +
geom_sf(data=sardinia_nuts2, aes(color = "nuts2"), fill = NA, show.legend = "line") +
geom_sf(data=start_pt, aes(color="points"), show.legend = "point") +
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
scale_color_manual(values = c(points = "red", nuts3= "#887e6a", nuts2 = "blue"),
label = c(points = 'Initiation points',
nuts3 = 'NUTS3 ',
nuts2 = 'NUTS2')) +
annotation_scale(location = "br",
width_hint = 0.5,
text_family = "serif",
bar_cols = c("#887e6a", "#fffff3")) +
annotation_north_arrow(location = "tr", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
)) +
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif", color="#887e6a") +
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA, size = 1.5),
axis.text=element_text(family = "serif", colour = "#887e6a"),
legend.key = element_rect(fill = "white"),
legend.title = element_blank()) +
guides(colour = guide_legend(override.aes = list(shape = c(NA, NA, 19), linetype = c(1, 1, NA))))
英文:
Here the full code and the map legend thanks to the help of @stefan
map <-ggplot() +
geom_sf(data=nuts3_italy, aes(color = "nuts3" ), fill = "antiquewhite", show.legend = "line")+
geom_sf(data=sardinia_nuts2, aes(color = "nuts2"),fill= NA, show.legend = "line") +
geom_sf(data=start_pt,aes(color="points"),show.legend = "point")+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
scale_color_manual(values = c(points = "red", nuts3= "#887e6a", nuts2 = "blue"),
label = c(points = 'Initiation points',
nuts3 = 'NUTS3 ',
nuts2 = 'NUTS2'))+
annotation_scale(location = "br",
width_hint = 0.5,
text_family = "serif",
bar_cols = c("#887e6a", "#fffff3")
) +
annotation_north_arrow(location = "tr", which_north = "true",
pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
style = north_arrow_fancy_orienteering(
fill = c("#887e6a", "#fffff3"),
line_col = "#887e6a",
text_family = "serif"
))+
annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
fontface = "italic", size = 5, fontface = "bold",
family = "serif",color="#887e6a" )+
labs(x="Longitude", y="Latitude") +
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
panel.background = element_rect(fill = "aliceblue"),
panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
axis.text=element_text(family = "serif",colour = "#887e6a"),
legend.key = element_rect(fill = "white"),
legend.title=element_blank()) +
guides(colour= guide_legend(override.aes = list(shape= c(NA, NA, 19),linetype=c(1,1,NA))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论