Reputation: 2351
I have code as follows, that creates a world map, where the countries light up green when they are present in the data.
# 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()
For the next step, I would like to use the observation per country to make gradations in the colouring (many observations darker and lower amount of observations lighter)
# 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]
To get what I want, I think I need to use geom_count
, like in this link.
The problem is that I am not sure how to combine this + geom_count(aes(color = ..n..))
code with the ggplot code I already have.
Could anyone show me where to start?
Upvotes: 0
Views: 585
Reputation: 2351
Thanks to NiklasvMoers..
(the fact that less countries light up is because of the imperfect merge)
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()
Upvotes: 2