-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap for world map.Rmd
123 lines (92 loc) · 3.56 KB
/
Map for world map.Rmd
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
---
title: "Map for world map"
author: "Grace Kim"
date: "11/30/2019"
output: html_document
---
This was a practice rmd for the world map leaflet, took many tries to get it fully done but it's here for everyone to enjoy!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# downloaded our various libraries
library(leaflet)
library(readr)
library(nnet)
library(rgdal)
library(janitor)
library(tidyverse)
```
```{r world map data cleaning}
# Read in the world religion data
world<- read_csv("Korea/raw-data/World_Religion_Data.csv", col_types = cols(
Country = col_character(),
Christian = col_double(),
Muslim = col_double(),
Unaffiliated = col_double(),
Hindu = col_double(),
Buddhist = col_double(),
Folk = col_double(),
Other = col_double(),
Jewish = col_double()
)) %>%
clean_names()
# added a column to this data that notes which religion is the highest for each country
# saved into a new data set that will have only country names and the religions
world_clean <- world %>%
mutate(highest_religion = apply(world[,-(1)],1,which.max)) %>%
select(country,highest_religion)
# change column names to join with other data set later
colnames(world)[1]<-"NAME"
# Read this shape file with the rgdal library.
world_spdf <- readOGR("Korea/raw-data/world_shape_file/TM_WORLD_BORDERS_SIMPL-0.3.shp", stringsAsFactors = F)
world_spdf@data[world_spdf@data$ISO2 == "AX","NAME"] <- "Aland Islands"
world_spdfog <- readOGR("Korea/raw-data/world_shape_file/TM_WORLD_BORDERS_SIMPL-0.3.shp", stringsAsFactors = F)
# joined the various religion data of the world with the shp file
world_spdf@data <- inner_join(world_spdf@data,world,by = "NAME")
```
```{r mappy time}
# created the yellow green blue color palette for the leaflet map
pal <- colorNumeric("YlGnBu", world_spdf@data$christian)
# the text popup labels for every country
# lists the religious percentages and the population and country name
mytext <- paste(
"Country: ", world_spdf@data$NAME,"<br/>",
"Religion: ", world_spdf@data$christian, "<br/>",
"Population: ", round(as.numeric(as.character(world_spdf@data$POP2005)), 2),
sep="") %>%
lapply(htmltools::HTML)
# actual leaflet!!!
# added the various nation tiles and the setview to the specific region on the map
leaflet(world_spdf)%>%
addTiles()%>%
setView(lat=10, lng=0 , zoom=2) %>%
# each polygon shape for each nation is noted below
addPolygons(stroke = FALSE,
fillOpacity = 0.9,
smoothFactor = 0.5,
color = ~pal(christian),
label = mytext,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "13px",
direction = "auto"
)) %>%
# legend for the various religious scales on the bottom right
addLegend("bottomright",
pal = pal,
values = ~christian,
title = "Religion",
opacity = 1)
```
```{r}
# practice graph for the world v.s. south korea grouped barplot
# use the rds file from the data cleaning rscript
vertical_world = read_rds("Korea/world.rds")
# not much data wrangling from here, just need to use ggplot and have the groups set
# using geom_bar position dodge and etc.
ggplot(vertical_world, aes(x = key, y=value, fill = country))+
geom_bar(position="dodge", stat="identity")+
labs(x = "Religion",
y = "Percentages of Religious Followers",
fill = "",
title = "The Religions of the World vs. South Korea")
```