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

孤者浪人 提交于 2021-02-05 07:27:07

问题


I'm trying to make a world map with a custom legend on the right side. The legend should be with the prepared text on the left and the generated numbers on the right. I tried but to no avail. I need help My code is as follows:

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"))

After running the code, the following world map is obtained:

How can I add a legend with free text entry? I would like the map to look like in this picture:


回答1:


This might help:

a) changing plot.margin,

b) adding geom_text for the annotation (updated with @Tung's suggestion to use check_overlap = TRUE to sharpen up the text), and

c) coord_cartesian(clip = 'off') to allow drawing outside of the plot area

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

Based on: ggplot2 - annotate outside of plot

Created on 2021-01-16 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/65744248/how-to-add-a-free-text-entry-as-a-legend-to-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!