-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.R
More file actions
103 lines (74 loc) · 3.26 KB
/
Script.R
File metadata and controls
103 lines (74 loc) · 3.26 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
# Set up the working directory for R to read and write data on your computer
setwd("C:/MappingCensusR_Data")
# Check the working directory is correct
getwd()
# List all the files in the working directory
dir()
# Install the rgdal package
install.packages ("rgdal",dependencies = TRUE) #make sure the another package(s) it probably depends on to be also installed
# Load the package "rgdal"
library(rgdal)
# Load the shapefile data and assign it to a new spatial object called "DA"
DA <- readOGR(dsn = "lda_000b16a_e", layer = "lda_000b16a_e")
# Get a quick summary of the data
summary (DA)
# Display the attribute table of the data as a spreadsheet
View(DA@data)
# Create a subset of this spatial object that only contains City of Windsor (whose CSDUID equal to 3537039) and only have one column "DAUID"
DA_Windsor <- DA[DA$CSDUID=="3537039", "DAUID"]
# Get the summary of the subset data and make a simple map
summary(DA_Windsor)
plot(DA_Windsor)
# Load the csv file and assign it to a new data frame
Census2016 <- read.csv("T1901EN.csv", header = TRUE)
# Get a summary of the data
summary(Census2016)
str(Census2016)
# Take a look at the data as a spreadsheet
View(Census2016)
# Create a subset of the data that only contains the desired columns
Sub_census <- Census2016[,c(1,7,9,12)]
# Fix column names containing spaces and commas
names(Sub_census)<-c("DAUID","POP","Dwelling","PopDen")
str(Sub_census)
# View and compare the attributes of the two data frames
View(Sub_census)
View(DA_Windsor@data)
# In order to join the two datasets, we need compatible versions of "DAUID" (the same data type)
class(DA_Windsor$DAUID)
class(Sub_census$DAUID)
# Install and load the package "dplyr"
install.packages("dplyr",dependencies = TRUE)
library(dplyr)
# join variables from the census data to the data slot of the spatial data frame
DA_Windsor@data <- left_join(DA_Windsor@data, Sub_census, by = "DAUID")
View(DA_Windsor@data)
# Install and load multiple packages
install.packages(c("RColorBrewer","classInt"),dependencies = TRUE)
lapply(c("RColorBrewer","classInt"), require, character.only = TRUE)
# Assign the variable you want to map
plotvar <- DA_Windsor$PopDen
# Assign the number of classes that the variable will be grouped into
nclr <- 5
# create a custom color palette for your map using the RColorBrewer package
# display a list of all the color palettes available
display.brewer.all()
# create a color palette object
plotclr <- brewer.pal(nclr, name = "YlOrRd")
# Decide the classification method(quantile) and obtain the break points for each class
q5 <- classIntervals(plotvar, nclr, style="quantile")
q5
# Obtain your color codes for each observation (DA)
q5Colours <- findColours(q5,plotclr)
str(q5Colours)
# Make a map
plot(DA_Windsor, col = q5Colours)
# Add a map title
title(main = "City of Windsor, 2016", sub = "By Census Dissemination Area")
# Create a custom legend for your map
legend(x = "topleft",# position of the legend
legend = names(attr(q5Colours, "table")),
fill = attr(q5Colours,"palette"),
bty = "n", #showing no box around the legend
title="Population density per square kilometer",
cex = 0.8) #adjust legend size