r - 在 ggplot 中添加国家/地区名称到 map

标签 r google-maps ggplot2 plot rworldmap

我尝试在ggplot中制作南美洲的 map 以及病例数。
我想添加国家/地区的名称。

但是,很难添加图例中的病例数和国家名称。

library(ggplot2)
library(dplyr)

sa.countries <- c('Argentina', 'Bolivia', 'Brazil',  
               'Colombia', 'Chile','Ecuador','French Guiana','Guyana', 
               'Paraguay', 'Peru', 'Suriname', 
               'Trinidad and Tobago', 'Uruguay', 'Venezuela')

countries.maps <- map_data("world", region = sa.countries)

country.cases <- tribble(~region, ~papers,
                          'Argentina', 33, 'Bolivia', 8, 'Brazil', 242,
                          'Colombia', 41, 'Chile', 9, 'Ecuador', 44,
                          'French Guiana', 3, 'Guyana', 0, 'Paraguay', 1,
                          'Peru', 8, 'Suriname', 0, 'Trinidad and Tobago', 2,
                          'Uruguay', 0, 'Venezuela', 7)

df.country <- left_join(countries.maps, country.cases, by="region")


ggplot(df.country, aes(long, lat, group = group))+
  geom_polygon(aes(fill = papers ), color = "white") +
  scale_fill_viridis_c(name='# cases', option = "C") +
  coord_equal() +
  theme_bw()

这是 map 的示例

enter image description here

提前致谢。

最佳答案

你可以试试

dff <- df.country %>%
  group_by(region) %>%
  summarize(long = mean(long, na.rm = T), lat = mean(lat, na.rm = T), group = group)
ggplot(df.country, aes(long, lat, group = group))+
  geom_polygon(aes(fill = papers ), color = "white") +
  scale_fill_viridis_c(name='# cases', option = "C") +
  coord_equal() +
  theme_bw() +
  geom_text(data = dff, aes(long, lat, label = region, group = group), size = 2)

enter image description here

关于r - 在 ggplot 中添加国家/地区名称到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72905784/

相关文章:

r - 如何防止 xlim 使用 geom_curve 改变高度?

r - 如何在 ggtern 中显示数据的真实值 (%)?

r - 避免将 alpha 美学应用于 ggplot2 中的 geom_text

r - 计算一段时间内的返回

javascript - 如何将 PHP 变量的值存储到 Javascript?

angular - 如何在 HTTPGET 上动态添加标记到谷歌地图组件(Angular 6)

javascript - Google Maps API v3,地理位置未正确返回

r - ggplot图例为什么显示 “colour”参数?

r - set.seed(123) 使用多少次

arrays - 了解 R 中的数组索引