在ggplot地图中添加自定义图例。

huangapple go评论115阅读模式
英文:

Add custom legend in ggplot map

问题

我在下面的撒丁岛地图上添加自定义图例时遇到了困难。这个图例应该包括颜色线条(NUTS2和NUTS3边界)和代表我进行大气模拟的起始点的颜色点。

以下是使用ggplot2构建撒丁岛地图的代码:

  1. # 获取意大利的NUTS2和NUTS3地区
  2. nuts2_italy <- gisco_get_nuts(year="2021",nuts_level = 2,country = "Italy")
  3. sardinia_nuts2 <- nuts2_italy[nuts2_italy$NUTS_NAME=='Sardegna',]
  4. nuts3_italy <- gisco_get_nuts(year="2021",nuts_level = 3,country = "Italy")
  5. # 加载起始点地图
  6. start_pt <- st_read("data/HYSPLIT_start_points/start_points.shp")
  7. # 绘制撒丁岛的起始点和NUTS边界
  8. install.packages("ggspatial")
  9. library(ggspatial)
  10. map <- ggplot(sardinia_nuts2) +
  11. geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
  12. geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
  13. geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
  14. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  15. annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
  16. annotation_north_arrow(location = "br", which_north = "true",
  17. pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
  18. style = north_arrow_fancy_orienteering(
  19. fill = c("#887e6a", "#fffff3"),
  20. line_col = "#887e6a",
  21. text_family = "serif"
  22. ))+
  23. annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
  24. fontface = "italic", size = 5, fontface = "bold",
  25. family = "serif",color="#887e6a" )+
  26. labs(x="Longitude", y="Latitude") +
  27. theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
  28. panel.background = element_rect(fill = "aliceblue"),
  29. panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
  30. axis.text=element_text(family = "serif",colour = "#887e6a"))

我得到了这张地图:

在ggplot地图中添加自定义图例。

我尝试在ggplot地图中添加"legend",代码如下:

  1. map <- ggplot(sardinia_nuts2) +
  2. geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
  3. geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
  4. geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
  5. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  6. annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
  7. annotation_north_arrow(location = "br", which_north = "true",
  8. pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
  9. style = north_arrow_fancy_orienteering(
  10. fill = c("#887e6a", "#fffff3"),
  11. line_col = "#887e6a",
  12. text_family = "serif"
  13. ))+
  14. annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
  15. fontface = "italic", size = 5, fontface = "bold",
  16. family = "serif",color="#887e6a" )+
  17. labs(x="Longitude", y="Latitude") +
  18. theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
  19. panel.background = element_rect(fill = "aliceblue"),
  20. panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
  21. axis.text=element_text(family = "serif",colour = "#887e6a")) +
  22. legend('topright', cex=1.2, legend=c('NUTS2 border', 'NUTS3 borders','study grid', 'Initiation points'),
  23. lty = c(1, 1, 2, NA), # tell are which objects to be drawn as a line in the legend
  24. pch=c(NA, NA, NA, 21), # tell are which objects to be drawn as a symbol
  25. col=c('black', 'black', 'black', 'black'), # color of the legend
  26. pt.bg=c('blue', "#887e6a", "#887e6a", "red"), # color of the symbol
  27. bty='n') #turn off the legend BORDER
  28. 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:

  1. # gets NUTS2 &amp; NUTS3 regions of Italy
  2. nuts2_italy &lt;- gisco_get_nuts(year=&quot;2021&quot;,nuts_level = 2,country = &quot;Italy&quot;)
  3. sardinia_nuts2 &lt;- nuts2_italy[nuts2_italy$NUTS_NAME==&#39;Sardegna&#39;,]
  4. nuts3_italy &lt;- gisco_get_nuts(year=&quot;2021&quot;,nuts_level = 3,country = &quot;Italy&quot;)
  5. # load initiation point map
  6. start_pt&lt;- st_read(&quot;data/HYSPLIT_start_points/start_points.shp&quot;)
  7. # map starting points and NUTS boundaries of Sardinia
  8. install.packages(&quot;ggspatial&quot;)
  9. library(ggspatial)
  10. map&lt;-ggplot(sardinia_nuts2) +
  11. geom_sf(data=nuts3_italy,fill = &quot;antiquewhite&quot;, color = &quot;#887e6a&quot; ,show.legend = &quot;line&quot;)+
  12. geom_sf(data=sardinia_nuts2, fill = NA ,color = &quot;blue&quot;,show.legend = &quot;line&quot;) +
  13. geom_sf(data=start_pt,color=&quot;red&quot;, size=1,show.legend = &quot;points&quot;)+
  14. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  15. annotation_scale(location = &quot;bl&quot;, width_hint = 0.5,text_family = &quot;serif&quot;,bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)) +
  16. annotation_north_arrow(location = &quot;br&quot;, which_north = &quot;true&quot;,
  17. pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
  18. style = north_arrow_fancy_orienteering(
  19. fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
  20. line_col = &quot;#887e6a&quot;,
  21. text_family = &quot;serif&quot;
  22. ))+
  23. annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;,
  24. fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
  25. family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
  26. labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +
  27. theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5),
  28. panel.background = element_rect(fill = &quot;aliceblue&quot;),
  29. panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
  30. axis.text=element_text(family = &quot;serif&quot;,colour = &quot;#887e6a&quot;))

I obtain this map

在ggplot地图中添加自定义图例。

I tried to add "legend" in my ggplot map as this:

  1. map&lt;-ggplot(sardinia_nuts2) +
  2. geom_sf(data=nuts3_italy,fill = &quot;antiquewhite&quot;, color = &quot;#887e6a&quot; ,show.legend = &quot;line&quot;)+
  3. geom_sf(data=sardinia_nuts2, fill = NA ,color = &quot;blue&quot;,show.legend = &quot;line&quot;) +
  4. geom_sf(data=start_pt,color=&quot;red&quot;, size=1,show.legend = &quot;points&quot;)+
  5. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  6. annotation_scale(location = &quot;bl&quot;, width_hint = 0.5,text_family = &quot;serif&quot;,bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)) +
  7. annotation_north_arrow(location = &quot;br&quot;, which_north = &quot;true&quot;,
  8. pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
  9. style = north_arrow_fancy_orienteering(
  10. fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
  11. line_col = &quot;#887e6a&quot;,
  12. text_family = &quot;serif&quot;
  13. ))+
  14. annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;,
  15. fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
  16. family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
  17. labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +
  18. theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5),
  19. panel.background = element_rect(fill = &quot;aliceblue&quot;),
  20. panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
  21. axis.text=element_text(family = &quot;serif&quot;,colour = &quot;#887e6a&quot;)) +
  22. legend(&#39;topright&#39;, cex=1.2, legend=c(&#39;NUTS2 border&#39;, &#39;NUTS3 borders&#39;,&#39;study grid&#39;, &#39;Initiation points&#39;),
  23. lty = c(1, 1, 2, NA), # tell are which objects to be drawn as a line in the legend
  24. pch=c(NA, NA, NA, 21), # tell are which objects to be drawn as a symbol
  25. col=c(&#39;black&#39;, &#39;black&#39;, &#39;black&#39;, &#39;black&#39;), # color of the legend
  26. pt.bg=c(&#39;blue&#39;, &quot;#887e6a&quot;, &quot;#887e6a&quot;, &quot;red&quot;), # color of the symbol
  27. bty=&#39;n&#39;) #turn off the legend BORDER
  28. 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的帮助:

  1. map <- ggplot() +
  2. geom_sf(data=nuts3_italy, aes(color = "nuts3" ), fill = "antiquewhite", show.legend = "line") +
  3. geom_sf(data=sardinia_nuts2, aes(color = "nuts2"), fill = NA, show.legend = "line") +
  4. geom_sf(data=start_pt, aes(color="points"), show.legend = "point") +
  5. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  6. scale_color_manual(values = c(points = "red", nuts3= "#887e6a", nuts2 = "blue"),
  7. label = c(points = 'Initiation points',
  8. nuts3 = 'NUTS3 ',
  9. nuts2 = 'NUTS2')) +
  10. annotation_scale(location = "br",
  11. width_hint = 0.5,
  12. text_family = "serif",
  13. bar_cols = c("#887e6a", "#fffff3")) +
  14. annotation_north_arrow(location = "tr", which_north = "true",
  15. pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
  16. style = north_arrow_fancy_orienteering(
  17. fill = c("#887e6a", "#fffff3"),
  18. line_col = "#887e6a",
  19. text_family = "serif"
  20. )) +
  21. annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia",
  22. fontface = "italic", size = 5, fontface = "bold",
  23. family = "serif", color="#887e6a") +
  24. labs(x="Longitude", y="Latitude") +
  25. theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5),
  26. panel.background = element_rect(fill = "aliceblue"),
  27. panel.border = element_rect(colour = "#887e6a", fill = NA, size = 1.5),
  28. axis.text=element_text(family = "serif", colour = "#887e6a"),
  29. legend.key = element_rect(fill = "white"),
  30. legend.title = element_blank()) +
  31. guides(colour = guide_legend(override.aes = list(shape = c(NA, NA, 19), linetype = c(1, 1, NA))))

这是带有图例的最终地图:
在ggplot地图中添加自定义图例。

英文:

Here the full code and the map legend thanks to the help of @stefan

  1. map &lt;-ggplot() +
  2. geom_sf(data=nuts3_italy, aes(color = &quot;nuts3&quot; ), fill = &quot;antiquewhite&quot;, show.legend = &quot;line&quot;)+
  3. geom_sf(data=sardinia_nuts2, aes(color = &quot;nuts2&quot;),fill= NA, show.legend = &quot;line&quot;) +
  4. geom_sf(data=start_pt,aes(color=&quot;points&quot;),show.legend = &quot;point&quot;)+
  5. coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  6. scale_color_manual(values = c(points = &quot;red&quot;, nuts3= &quot;#887e6a&quot;, nuts2 = &quot;blue&quot;),
  7. label = c(points = &#39;Initiation points&#39;,
  8. nuts3 = &#39;NUTS3 &#39;,
  9. nuts2 = &#39;NUTS2&#39;))+
  10. annotation_scale(location = &quot;br&quot;,
  11. width_hint = 0.5,
  12. text_family = &quot;serif&quot;,
  13. bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)
  14. ) +
  15. annotation_north_arrow(location = &quot;tr&quot;, which_north = &quot;true&quot;,
  16. pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
  17. style = north_arrow_fancy_orienteering(
  18. fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
  19. line_col = &quot;#887e6a&quot;,
  20. text_family = &quot;serif&quot;
  21. ))+
  22. annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;,
  23. fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
  24. family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
  25. labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +
  26. theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5),
  27. panel.background = element_rect(fill = &quot;aliceblue&quot;),
  28. panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
  29. axis.text=element_text(family = &quot;serif&quot;,colour = &quot;#887e6a&quot;),
  30. legend.key = element_rect(fill = &quot;white&quot;),
  31. legend.title=element_blank()) +
  32. guides(colour= guide_legend(override.aes = list(shape= c(NA, NA, 19),linetype=c(1,1,NA))))

Here is the final map with the legend:
在ggplot地图中添加自定义图例。

huangapple
  • 本文由 发表于 2023年8月8日 21:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定