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

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

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地图中添加自定义图例。

我尝试在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 &amp; NUTS3 regions of Italy
nuts2_italy &lt;- gisco_get_nuts(year=&quot;2021&quot;,nuts_level = 2,country = &quot;Italy&quot;)
sardinia_nuts2 &lt;- nuts2_italy[nuts2_italy$NUTS_NAME==&#39;Sardegna&#39;,]
nuts3_italy &lt;- gisco_get_nuts(year=&quot;2021&quot;,nuts_level = 3,country = &quot;Italy&quot;)
# load initiation point map
start_pt&lt;- st_read(&quot;data/HYSPLIT_start_points/start_points.shp&quot;)
# map starting points and NUTS boundaries of Sardinia
install.packages(&quot;ggspatial&quot;)
library(ggspatial)
map&lt;-ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = &quot;antiquewhite&quot;, color = &quot;#887e6a&quot; ,show.legend = &quot;line&quot;)+
geom_sf(data=sardinia_nuts2, fill = NA ,color = &quot;blue&quot;,show.legend = &quot;line&quot;) +
geom_sf(data=start_pt,color=&quot;red&quot;, size=1,show.legend = &quot;points&quot;)+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = &quot;bl&quot;, width_hint = 0.5,text_family = &quot;serif&quot;,bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)) +
annotation_north_arrow(location = &quot;br&quot;, which_north = &quot;true&quot;, 
pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
style = north_arrow_fancy_orienteering(
fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
line_col = &quot;#887e6a&quot;,
text_family = &quot;serif&quot;
))+
annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;, 
fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +
theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5), 
panel.background = element_rect(fill = &quot;aliceblue&quot;),
panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
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:

map&lt;-ggplot(sardinia_nuts2) +
geom_sf(data=nuts3_italy,fill = &quot;antiquewhite&quot;, color = &quot;#887e6a&quot; ,show.legend = &quot;line&quot;)+
geom_sf(data=sardinia_nuts2, fill = NA ,color = &quot;blue&quot;,show.legend = &quot;line&quot;) +
geom_sf(data=start_pt,color=&quot;red&quot;, size=1,show.legend = &quot;points&quot;)+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
annotation_scale(location = &quot;bl&quot;, width_hint = 0.5,text_family = &quot;serif&quot;,bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)) +
annotation_north_arrow(location = &quot;br&quot;, which_north = &quot;true&quot;, 
pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
style = north_arrow_fancy_orienteering(
fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
line_col = &quot;#887e6a&quot;,
text_family = &quot;serif&quot;
))+
annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;, 
fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +
theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5), 
panel.background = element_rect(fill = &quot;aliceblue&quot;),
panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
axis.text=element_text(family = &quot;serif&quot;,colour = &quot;#887e6a&quot;)) +
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;),
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(&#39;black&#39;, &#39;black&#39;, &#39;black&#39;, &#39;black&#39;), # color of the legend
pt.bg=c(&#39;blue&#39;, &quot;#887e6a&quot;, &quot;#887e6a&quot;, &quot;red&quot;), # color of the symbol
bty=&#39;n&#39;)  #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))))

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

英文:

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

 map &lt;-ggplot() +
geom_sf(data=nuts3_italy, aes(color = &quot;nuts3&quot; ), fill = &quot;antiquewhite&quot;, show.legend = &quot;line&quot;)+
geom_sf(data=sardinia_nuts2, aes(color = &quot;nuts2&quot;),fill= NA, show.legend = &quot;line&quot;) +
geom_sf(data=start_pt,aes(color=&quot;points&quot;),show.legend = &quot;point&quot;)+
coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +  
scale_color_manual(values = c(points = &quot;red&quot;, nuts3= &quot;#887e6a&quot;, nuts2 = &quot;blue&quot;),
label = c(points = &#39;Initiation points&#39;,
nuts3 = &#39;NUTS3 &#39;,
nuts2 = &#39;NUTS2&#39;))+  
annotation_scale(location = &quot;br&quot;, 
width_hint = 0.5,
text_family = &quot;serif&quot;,
bar_cols = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;)
) +  
annotation_north_arrow(location = &quot;tr&quot;, which_north = &quot;true&quot;, 
pad_x = unit(0.75, &quot;cm&quot;), pad_y = unit(0.2, &quot;cm&quot;),
style = north_arrow_fancy_orienteering(
fill = c(&quot;#887e6a&quot;, &quot;#fffff3&quot;),
line_col = &quot;#887e6a&quot;,
text_family = &quot;serif&quot;
))+  
annotate(geom = &quot;text&quot;, x = 9, y = 40.7, label = &quot;Sardinia&quot;, 
fontface = &quot;italic&quot;, size = 5, fontface = &quot;bold&quot;,
family = &quot;serif&quot;,color=&quot;#887e6a&quot; )+
labs(x=&quot;Longitude&quot;, y=&quot;Latitude&quot;) +  
theme(panel.grid.major = element_line(color = gray(.5), linetype = &quot;dashed&quot;, size = 0.5), 
panel.background = element_rect(fill = &quot;aliceblue&quot;),
panel.border = element_rect(colour = &quot;#887e6a&quot;, fill = NA,size = 1.5),
axis.text=element_text(family = &quot;serif&quot;,colour = &quot;#887e6a&quot;),
legend.key = element_rect(fill = &quot;white&quot;),
legend.title=element_blank()) +
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:

确定