top of page
Writer's pictureKarl 曹

Data Visualization Practice for Map

Here are my data visualization works on the rice export data 2020, and the data source is :




library(tidyverse)
library("ggplot2")
library(dplyr)
rm(list=ls())
fooddata <- read.csv("D:/Master/RA ZEF/FAOSTAT_data_en_11-22-2022.csv")
summary(fooddata)
mapdata <- map_data("world") 

working_data <- fooddata %>% filter(Element == "Export Quantity", Item =="Rice")

working_data <- working_data[,c('Area','Value')]
aaa <- count(mapdata['region'])
## aaa is the country names in map data
result <- c()
for (i in 1:112) {
  result[i] <- filter(aaa,str_detect(aaa[,1],working_data[i,1]))[1]
}

result <- unlist(result)
difference <- setdiff(working_data[,1],result)

view(difference)

filter(aaa,str_detect(aaa[,1],'Congo'))

#filter(working_data,str_detect(working_data[,1],'Tu'))

## change name to map data
# note that Tango value is 0, omit it
working_data <- working_data%>%
  mutate(region=recode(working_data[,1], "China, mainland"="China",
                  "Congo"='Republic of Congo',
                 "C么te d'Ivoire"='Ivory Coast',
                "Czechia"='Czech Republic',
                 "Eswatini"='Swaziland',
               "Iran (Islamic Republic of)"='Iran',
                "Lao People's Democratic Republic"='Laos',
               "Republic of Korea"='South Korea',
                 "Russian Federation"='Russia',
                  "Trinidad and Tobago"='Trinidad',
                 "United Republic of Tanzania"='Tanzania',
                 "United States of America"='USA',
                 'Viet Nam'='Vietnam',
       'T眉rkiye'='Turkey',
               'United Kingdom of Great Britain and Northern Ireland'='UK'
  ))

working_data <- working_data[,c(2,3)]


mapdata2 <- left_join(mapdata,working_data,by='region')

mapdata3<-mapdata2 %>% 
  filter(!is.na(mapdata2$Value))
View(mapdata3)


countries <- mapdata2%>%
  filter(!is.na(Value))%>%
  group_by(region)%>%
  dplyr::summarize(long = mean(long,na.rm = T), lat = mean(lat,na.rm = T))%>%
  ungroup()

countries[107,2] <- -100
countries[107,3] <- 40

countries[16,2] <- -90
countries[16,3] <- 60


countries2 <- left_join(countries,working_data,
                        by="region")
countries2['logv'] <- sqrt(countries2['Value'])+1

map1<-ggplot(mapdata2, aes( x = long, y = lat, group=group)) +
  geom_polygon(aes(fill = log(Value)), color = "black")
map1

map2 <- map1 + scale_fill_gradient(name = "Log Tones", low = "gold", high ="chocolate4", na.value = "grey50")+
  ggtitle("2020 World Rice Export Quantity (log value in tones)")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        rect = element_blank(),
        panel.background = element_rect(fill = "#99CCFF"),
        plot.title = element_text(hjust = 0.5)) +
  labs(caption = "Source: FAOSTAT")+ 
  geom_text(data = countries2, aes(long, lat, size=logv,
                                   label = region,group=region))
map2
### optional projection orthographic, "cylequalarea",lat0=90, 

#############################################
countries <- mapdata2%>%
  filter(!is.na(Value))%>%
  group_by(region)%>%
  dplyr::summarize(long = mean(long,na.rm = T), lat = mean(lat,na.rm = T))%>%
  ungroup()

map1<-ggplot(mapdata2, aes( x = long, y = lat, group=group)) +
  geom_polygon(aes(fill = log(Value)), color = "black")
map1


map3 <- map1 + scale_fill_gradient(name = "Log Tones", low = "gold", high ="chocolate4", na.value = "grey")+
  #ggtitle("2020 World Rice Export Quantity (log value in tones)")+
  labs(title = "2020 World Rice Export Quantity (log value in tones)",
    caption = "Source: FAOSTAT")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        rect  = element_blank(),
        plot.title = element_text(hjust = 0.5,size = 50),
        plot.caption = element_text(size = 50),
        panel.grid = element_line(color = "dodgerblue4",
                                  size = 0.05,
                                  linetype = 1))+
  coord_map("ortho", orientation=c(51, 10, 0))+ 
  scale_y_continuous(breaks = (-100:100) * 5) +
  scale_x_continuous(breaks = (-100:100) * 5) +
  geom_text(data = countries, aes(long, lat, label = region,group=region), size = 6)
map3
# export with 3900*3000

69 views0 comments

Recent Posts

See All

Comments


bottom of page