I need help. What I'd like to achieve is to create a map with selected countries (20 unique countries) highlighted and colour coded according to their level of income. I'd like to keep the remaining countries. I've tried reading elsewhere and I am not sure how to move forward.
I've tried:
Here's my data:
structure(list(region = c("Belgium", "Canada", "Cyprus", "Democratic Republic of the Congo", "Denmark", "Ghana", "Greece", "India", "Israel", "Italy", "Kenya", "Malaysia", "Nigeria", "Philippines", "Portugal", "Spain", "Sri Lanka", "Tanzania", "UK", "USA"), `Country by level of income (World Bank)` = c("High", "High", "High", "Low", "High", "Lower middle-income", "High", "Lower middle-income", "High", "High", "Lower middle-income",
"Upper middle income", "Lower middle-income", "Lower middle-income", "High", "High", "Lower middle-income", "Lower middle-income", "High", "High"), Continent = c("Northern Europe", "North America", "Mediterranean", "Africa", "Northern Europe", "Africa", "Mediterranean",
"South Asia", "Middle East", "Mediterranean", "Africa", "South East Asia", "Africa", "South East Asia", "Mediterranean", "Mediterranean", "South Asia", "Africa", "Northern Europe", "North America")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L))
My code:
library(dplyr)
library(stringr)
library(ggplot2)
library(maps)
library(ggmaps)
library(scales)
library(sf)
library(readxl)
library(extrafont)
options(scipen = 999)
world <- map_data("world")
worldplot <- ggplot() +
geom_polygon(data = world, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3)
worldplot
country_income_map <- left_join(world, country_income, by = "region")
View(country_income_map)
country_income_map <- mutate_at(country_income_map, vars('Country by level of income (World Bank)',
'region'), as.factor)
custom_colour_scale_income <- c("High"='#404E88', "Upper middle income" = '#2A8A8C',
"Lower middle-income" = '#7FD157', "Low" = '#F9E53F')
ggplot(country_income_map, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(fill = "Country by level of income (World Bank)")) +
scale_fill_manual(values = custom_colour_scale_income) +
guides(fill = guide_legend(reverse = T)) +
labs(fill = 'Level of income'
,title = 'Responses by country and level of income'
,x = NULL
,y = NULL) +
theme(text = element_text(family="Gill Sans MT", color = '#EEEEEE')
,plot.title = element_text(size = 28)
,plot.subtitle = element_text(size = 14)
,axis.ticks = element_blank()
,axis.text = element_blank()
,panel.grid = element_blank()
,panel.background = element_rect(fill = '#333333')
,plot.background = element_rect(fill = '#333333')
,legend.position = c(.18,.36)
,legend.background = element_blank()
,legend.key = element_blank()
)
My current output:
A tiny change will make your colors show up. Since you have an illegal column name (name with spaces), you have to add back ticks around it not quotation marks in your geom_polygon()
country_income <-
structure(
list(
region = c(
"Belgium",
"Canada",
"Cyprus",
"Democratic Republic of the Congo",
"Denmark",
"Ghana",
"Greece",
"India",
"Israel",
"Italy",
"Kenya",
"Malaysia",
"Nigeria",
"Philippines",
"Portugal",
"Spain",
"Sri Lanka",
"Tanzania",
"UK",
"USA"
),
`Country by level of income (World Bank)` = c(
"High",
"High",
"High",
"Low",
"High",
"Lower middle-income",
"High",
"Lower middle-income",
"High",
"High",
"Lower middle-income",
"Upper middle income",
"Lower middle-income",
"Lower middle-income",
"High",
"High",
"Lower middle-income",
"Lower middle-income",
"High",
"High"
),
Continent = c(
"Northern Europe",
"North America",
"Mediterranean",
"Africa",
"Northern Europe",
"Africa",
"Mediterranean",
"South Asia",
"Middle East",
"Mediterranean",
"Africa",
"South East Asia",
"Africa",
"South East Asia",
"Mediterranean",
"Mediterranean",
"South Asia",
"Africa",
"Northern Europe",
"North America"
)
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-20L)
)
library(dplyr)
library(stringr)
library(ggplot2)
library(maps)
library(ggmaps)
library(scales)
library(sf)
library(readxl)
library(extrafont)
options(scipen = 999)
world <- map_data("world")
worldplot <- ggplot() +
geom_polygon(data = world, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3)
worldplot
country_income_map <- left_join(world, country_income, by = "region")
View(country_income_map)
country_income_map <- mutate_at(country_income_map, vars('Country by level of income (World Bank)',
'region'), as.factor)
custom_colour_scale_income <- c("High"='#404E88', "Upper middle income" = '#2A8A8C',
"Lower middle-income" = '#7FD157', "Low" = '#F9E53F')
ggplot(country_income_map, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(fill = `Country by level of income (World Bank)`)) +
scale_fill_manual(values = custom_colour_scale_income) +
guides(fill = guide_legend(reverse = T)) +
labs(fill = 'Level of income'
,title = 'Responses by country and level of income'
,x = NULL
,y = NULL) +
theme(
# text = element_text(family="Gill Sans MT", color = '#EEEEEE')
,plot.title = element_text(size = 28)
,plot.subtitle = element_text(size = 14)
,axis.ticks = element_blank()
,axis.text = element_blank()
,panel.grid = element_blank()
,panel.background = element_rect(fill = '#333333')
,plot.background = element_rect(fill = '#333333')
,legend.position = c(.18,.36)
,legend.background = element_blank()
,legend.key = element_blank()
)