簡體   English   中英

使用 ggplot 為世界 map 的特定國家/地區添加顏色

[英]Adding colour to specific countries in a world map using ggplot

我正在嘗試使用 ggplot 生成世界 map 的 plot,其中美國、加拿大和巴西為紅色。

這是我嘗試過的

thismap = map_data("world")

ggplot(thismap, aes(long, lat, group=group))+geom_polygon(fill="white", colour="gray")+
  ggtitle("Map of World")

但是,我無法獲得那些紅色的國家

嘗試這個。 我在 df 中添加一列,在其中設置 colors。 ggplot map 里面fill審美(.!)。 要獲得正確的 colors 使用scale_fill_identity

library(ggplot2)
library(dplyr)

thismap = map_data("world")

# Set colors
thismap <- mutate(thismap, fill = ifelse(region %in% c("Brazil", "Canada", "USA"), "red", "white"))

# Use scale_fiil_identity to set correct colors
ggplot(thismap, aes(long, lat, fill = fill, group=group)) + 
  geom_polygon(colour="gray") + ggtitle("Map of World") + 
  scale_fill_identity()

reprex package (v0.3.0) 於 2020 年 4 月 5 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM