-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data-Explore.Rmd
106 lines (89 loc) · 2.87 KB
/
Data-Explore.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
---
title: "Data-Explore"
author: "Vanessa Tobias <[email protected]>"
date: "7/27/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Load the libraries
```{r}
library(ggplot2)
library(cowplot)
```
# Set up the data
We just want to use 2010 data for now because it includes the largest number of variables. We also want to fill in the elevation for all years.
```{r}
master <- read.csv("master.csv", header = TRUE, stringsAsFactors = FALSE)
master2010 <- master[master$year == 2010,]
```
# Make some plots
```{r multiple plot function}
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
#http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
```
```{r}
p1 <- ggplot(master2010, aes(SoCI_bond, pop_change1)) +
geom_point() +
geom_hline(yintercept = 0) +
theme_cowplot() +
xlab("Bonding") +
ylab("Population Change (%)")
p2 <- ggplot(master2010, aes(SoCI_bridge, pop_change1)) +
geom_point() +
geom_hline(yintercept = 0) +
theme_cowplot() +
xlab("Bridging") +
ylab("Population Change (%)")
p3 <- ggplot(master2010, aes(SoCI_bond, pop_change2)) +
geom_point() +
geom_hline(yintercept = 0) +
theme_cowplot() +
xlab("Bonding") +
ylab("Population Change (%)")
p4 <- ggplot(master2010, aes(SoCI_bridge, pop_change2)) +
geom_point() +
geom_hline(yintercept = 0) +
theme_cowplot() +
xlab("Bridging") +
ylab("Population Change (%)")
multiplot(p1, p3, p2, p4, cols=2)
```