代码之家  ›  专栏  ›  技术社区  ›  Kirds

在ggplot中将国家名称添加到地图中

  •  0
  • Kirds  · 技术社区  · 2 年前

    我试着在 ggplot
    我想加上这些国家的名字。

    然而,很难在图例中添加案件数量和国家名称。

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

    enter image description here

    提前谢谢。

    1 回复  |  直到 2 年前
        1
  •  3
  •   Park    2 年前

    你可以试试

    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