-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical4_homework.Rmd
More file actions
126 lines (98 loc) · 3.21 KB
/
practical4_homework.Rmd
File metadata and controls
126 lines (98 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
title: "Practical4 Homework"
author: "xu"
date: "2023-10-27"
output: html_document
---
load the packages
```{r setup, include=FALSE}
library(tidyverse)
library(janitor)
library(sf)
library(countrycode)
library(tmaptools)
library(tmap)
library(RSQLite)
```
Load the csv and shape files
```{r}
#load the csv file and assign na to all null values
#the data set has been renamed as 'homework_dataset' and it's under the directory 'datafolder'
dataset <- read_csv("datafolder/homework_dataset.csv",na=" ") %>%
dplyr::rename(index_2010="gii_2010",index_2019="gii_2019")%>%
select(country,index_2010,index_2019,iso3)%>%
mutate(iso_code=countrycode(country, origin = 'country.name', destination = 'iso2c'))%>%
mutate(iso_code2=countrycode(iso3, origin ='iso3c', destination = 'iso2c'))
shapefile <- st_read("World_Countries_Generalized/World_Countries_Generalized.shp")
```
```{r}
dataset <- na.omit(dataset)
#remove the NAs in dataset
newdataset <- dataset %>%
mutate(difference=(index_2019-index_2010))
# create a new column using the index of 2010 subtract from the index of 2019
joined_data <- shapefile %>%
left_join(.,
newdataset,
by = c("ISO" = "iso_code"))
# left join the data
```
```{r}
tmap_mode("plot")
qtm(joined_data,
fill = "difference")
``
```
```{r}
breaks=c(0.0,0.2,0.4,0.6,0.8,1.0)
diffbreaks=c(-0.4,-0.3,-0.2,-0.1,0,0.1)
# preserve size not direction like WGS84
joinshp = st_transform(joined_data, crs = "+proj=moll")
# plot each map
tm1 <- tm_shape(joinshp) +
tm_polygons("index_2019",
breaks=breaks,
palette="PuBu")+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(a)", position=c(0,0.85), size=1.5)
tm2 <- tm_shape(joinshp) +
tm_polygons("index_2010",
breaks=breaks,
palette="PuBu") +
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(b)", position=c(0,0.85), size=1.5)
tm3 <- tm_shape(joinshp) +
tm_polygons("difference",
#style="fixed",
breaks=diffbreaks,
palette=("Blues"),
midpoint = NA)+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(c)", position=c(0,0.85), size=1.5)
legend <- tm_shape(joinshp) +
tm_polygons("index_2019",
breaks=breaks,
palette="PuBu",
title = "GII")+
tm_legend(show=TRUE)+
#asp is aspect ratio!
tm_layout(legend.only = TRUE, legend.position=c(0.3,0.25),asp=0.1)+
tm_shape(joinshp) +
tm_polygons("difference",
palette=("Blues"),
midpoint = NA,
title="HDI difference \n(2019-2010)") +
#tm_scale_bar(position=c(0.2,0.04), text.size=0.6)+
tm_compass(north=0, position=c(0.6,0.6))+
tm_layout(legend.only = TRUE, legend.position=c(0.1,0.1),asp=0.1)+
tm_credits("Mapped data:\nUN Human Development Index\nWorld outline:\nArcGIS Hub
", position=c(0.35,0.2), just="left")
# in tmap can't make legends side by side unless use this workaround:
#https://github.com/r-tmap/tmap/issues/476
t=tmap_arrange(tm1, tm2, tm3, legend, ncol=2)
t
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.