Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

China not showing on chloropleth map in R

Tags:

r

maps

ggplot2

I am trying to make a simple chloropleth map in r. While I can create the map, China does not show up at all, leaving a giant hole in the map. I have checked that China is in the data, so I am not sure what is going on. here is my code:

library(tidyverse)
library(maps)

## load data ----
data(country.map, package = "choroplethrMaps")
co2 <- read.csv("https://raw.githubusercontent.com/acircleda/tmp/master/food emissions.csv") %>% 
  mutate(region = tolower(country))

data<-co2 %>% 
  left_join(country.map, by="region") %>%
  group_by(region) %>% mutate(
    c02sum = sum(co2_emmission)
  )

ggplot(data, aes(long, lat, group = group))+
  geom_polygon(aes(fill = c02sum), color = "white", na="grey80")+
  scale_fill_viridis_c(option = "C")

##no china

china<-data %>% filter(region == "china") ##china is in the data

nochina

like image 835
acircleda Avatar asked Nov 22 '25 20:11

acircleda


1 Answers

The problem is your data join. What you actually want, is a right_join. NAs can be coloured in your scale_fill functions with na.value = .

library(tidyverse)
library(maps)

## load data ----
data(country.map, package = "choroplethrMaps")
co2 <- read_csv('your.csv')%>% 
  mutate(region = tolower(country))
                               
data <- co2 %>% 
  right_join(country.map, by="region") %>% #critical
  group_by(region) 

ggplot(data, aes(long, lat, group = group))+
  geom_polygon(aes(fill = co2_emmission), na.rm ="grey80") +
  scale_fill_continuous(na.value = 'grey')

like image 78
tjebo Avatar answered Nov 24 '25 11:11

tjebo