簡體   English   中英

根據 ggplot2 世界 map 中的觀察創建顏色漸變

[英]Creating colour gradients based on observations in a ggplot2 world map

我有如下代碼,它創建了一個世界 map,當國家/地區出現在數據中時,它們會亮起綠色。

# List of countries
library(countrycode)
library(ggplot2)
library(rvest)
library(data.table)
library(dplyr)
library(tidyverse)

# Loads world map
worldmap <- map_data("world")
# Vector of countries in the European Union
European_Union <- c("Austria", "Belgium", "Bulgaria", "Croatia", "Republic of Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden")
# Unique world map for every data source
worldmap_AMIS_policy <- worldmap
url <- 'http://statistics.amis-outlook.org/policy/doc/query_download/Bulk_AllData.zip'
download.file(url, "AMIS_policy.zip")
unzip("AMIS_policy.zip")
AMIS_policy_dat <- read.csv("M:/My Documents/Bulk_AllData/Bulk_AllData_policy.csv")
# Keep unique country names
vec_AMIS_policy <- unique(AMIS_policy_dat$Country_Name)

# Add all EU countries if EU is available
if ("EU" %in% vec_AMIS_policy | "European Union" %in% vec_AMIS_policy) {
    vec_AMIS_policy <- append(vec_AMIS_policy, European_Union)
}

# Set colors
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Ascension Island, Azores, Barbuda, Canary Islands, Chagos Archipelago, Grenadines, Heard Island, Madeira Islands, Micronesia, Saba, Saint Martin, Siachen Glacier, Sint Eustatius, Virgin Islands
worldmap_AMIS_policy <- mutate(worldmap_AMIS_policy, region = countryname(region), fill = ifelse(region %in% countryname(vec_AMIS_policy), "green", "lightgrey"))

# Use scale_fiil_identity to set correct colors
ggplot(worldmap_AMIS_policy, aes(long, lat, fill = fill, group=group)) + 
  geom_polygon(colour="gray") + ggtitle("Map of World") + 
  ggtitle("Availability of AMIS Policy Data - Monthly") +
  scale_fill_identity()

在此處輸入圖像描述

對於下一步,我想使用每個國家/地區的觀察結果來進行着色漸變(許多觀察結果更暗,更少的觀察結果更亮)

# Count the number of observations by country
setDT(AMIS_policy_dat)[PolicyType_Name=="Export measures", if_1_is_export:=1]
setDT(AMIS_policy_dat)[PolicyType_Name=="Import measures", if_1_is_export:=0]
policy_count <- setDT(AMIS_policy_dat)[, .(count = .N, var = sum(if_1_is_export)), by = Country_Name]

為了得到我想要的,我想我需要使用geom_count ,就像在這個鏈接中一樣。

問題是我不確定如何將此+ geom_count(aes(color =..n..))代碼與我已經擁有的 ggplot 代碼結合起來。

誰能告訴我從哪里開始?

感謝 NiklasvMoers..

(點亮的國家較少是因為合並不完善)

setDT(worldmap_AMIS_policy)[, Country_Name:=region]
worldmap_AMIS_policy <- merge(worldmap_AMIS_policy,policy_count, all.x=TRUE, by=c("Country_Name"))

worldmap_AMIS_policy <- mutate(worldmap_AMIS_policy, region = countryname(region), fill = ifelse(region %in% countryname(vec_AMIS_policy), "green", "lightgrey"))

setnames(worldmap_AMIS_policy, "count", "Policy Count")

# Use scale_fiil_identity to set correct colors
ggplot(worldmap_AMIS_policy, aes(long, lat, fill = `Policy Count`, group=group)) + 
  geom_polygon(colour="gray") + ggtitle("Map of World") + 
  ggtitle("Availability of AMIS Policy Data - Monthly") +
  scale_fill_gradient()

在此處輸入圖像描述

暫無
暫無

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

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