如何将自由文本条目作为图例添加到 ggplot?

How to add a free text entry as a legend to ggplot?

我正在尝试制作一张右侧带有自定义图例的世界地图。图例应在左侧显示准备好的文本,在右侧显示生成的数字。我试过了但无济于事。我需要帮助我的代码如下:

library(dplyr)
library(ggplot2)
library(ggrepel)
library(rworldmap)

world_map_without_antarctica <- getMap()[-which(getMap()$ADMIN=='Antarctica'),]  #get data from web
world_map_without_antarctica_t <- fortify(world_map_without_antarctica)


Data <- data.frame( "lon"=c(17.816883, 38.544239,20.895352,20.819651,35.392102,99.060241,
                            43.756911, 10.288485, 16.566191, 14.076159,8.118301,16.596266,
                            121.544442,-73.077732,14.938152),
                    
                    "lat"=c(44.1807678, 35.0126143, 42.5793648, 44.2330372, 38.9907297, 
                            39.5015541, 33.0368223, 51.1337227, 45.0162344, 47.6139488, 
                            46.7917377, 62.8114850, 15.7509443, 3.9272139, 46.1254221), 
                    "NAME"=c("Bosnia and Herzegovina", "Syria", "Kosovo","Republic of Serbia",
                             "Turkey","United States of America","Iraq","Germany","Croatia",
                             "Austria","Switzerland","Sweden","Philippines","Colombia","Slovenia"), 
                    "Count"=c(65800,32636,15005,9276,6979,6528,6449,
                              5830,4862,3109,2959,2777,2577,2315,1394))


Data$label <- paste0(Data$NAME,': ',Data$Count)
word_data_merged <- merge(world_map_without_antarctica_t, Data[ , c("NAME","Count")], by.x="id", by.y="NAME", all.x=T)
word_data_merged <- word_data_merged %>% arrange(id, order)

country_shapes <- geom_polygon(data = world_map_without_antarctica_t, aes(x = long, y = lat, group = group),fill = NA)

maptheme <- 
  theme(panel.grid = element_blank())+
  theme(axis.text = element_blank())+
  theme(axis.ticks = element_blank())+
  theme(axis.title = element_blank())+
  theme(legend.position = "bottom")+
  theme(panel.grid = element_blank())+
  theme(plot.margin = unit(c(0, 0, 0.5, 0), 'cm'))


guide = guide_colorbar(
  title="legend_title",
  label = TRUE,
  draw.ulim = TRUE,
  draw.llim = TRUE,
  frame.colour = "black",
  ticks = TRUE,
  nbin = 10,
  label.position = "bottom",
  barwidth = 13,
  barheight = 1.3,
  direction = 'horizontal')


ggplot(word_data_merged) + 
  labs(title = "plot_title", subtitle = "plot_subtitle") +
  country_shapes +
  scale_fill_gradient(high = "#381802", low = "#fccaa7", guide = guide) +
  geom_polygon(aes(long, lat, group = group, fill=Count),alpha=1) + 
  geom_point(data=Data[Data$label !="",],aes(x = lon, y = lat), shape = 21,fill= "#275083", color = "#275083", size = 1.5,alpha=0.5) +
  geom_path(aes(x=long,y=lat, group = group), color="#c7c9c9", size= 0.5, alpha=0.4) + 
  geom_label_repel(data=Data,aes(x= lon,y= lat,label = label),
                   size = 5, 
                   show.legend= F,
                   fontface = "bold",
                   point.padding = unit(0.2, "lines") ) +
  maptheme +
  theme(panel.background = element_rect(fill = "#ebf2f7"))

经过运行代码得到如下世界地图:

如何添加带有自由文本输入的图例?我希望地图看起来像这张图片:

这可能有帮助:

a) 改变 plot.margin,

b) 为注释添加 geom_text(根据@Tung 的建议更新,使用 check_overlap = TRUE 来锐化文本),以及

c) coord_cartesian(clip = 'off') 允许在绘图区域外绘图

ggplot(word_data_merged) + 
  labs(title = "plot_title", subtitle = "plot_subtitle") +
  country_shapes +
  scale_fill_gradient(high = "#381802", low = "#fccaa7", guide = guide) +
  geom_polygon(aes(long, lat, group = group, fill=Count),alpha=1) + 
  geom_point(data=Data[Data$label !="",],aes(x = lon, y = lat), shape = 21,fill= "#275083", color = "#275083", size = 1.5,alpha=0.5) +
  geom_path(aes(x=long,y=lat, group = group), color="#c7c9c9", size= 0.5, alpha=0.4) + 
  geom_label_repel(data=Data,aes(x= lon,y= lat,label = label),
                   size = 5, 
                   show.legend= F,
                   fontface = "bold",
                   point.padding = unit(0.2, "lines") ) +
  geom_text(aes(label = "Statistics", x = 180, y = 80),
            fontface = "bold",
            hjust = -0.5,
            size = 5,
            check_overlap = TRUE) +
  geom_text(aes(label = "Total unique countries = #", x = 180, y = 70),
            hjust = -0.35,
            size = 3,
            check_overlap = TRUE) +
  coord_cartesian(clip = 'off')+
  maptheme +
  theme(panel.background = element_rect(fill = "#ebf2f7"),
        plot.margin = unit(c(0, 4, 0.5, 0), 'cm'))
#> Warning: ggrepel: 12 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

基于:ggplot2 - annotate outside of plot

reprex package (v0.3.0)

于 2021 年 1 月 16 日创建