-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_1 - Biodynamic Data Exploration.Rmd
228 lines (164 loc) · 5.82 KB
/
1_1 - Biodynamic Data Exploration.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: "1.1 - Biodynamic Data Exploration"
author: "Julian Barg"
date: "January 20, 2019"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## 1. Load data
```{r load, message=FALSE}
library(tidyverse)
library(readr)
biodynamic <- read_csv("downloads/biodynamic_history.csv")
glimpse(biodynamic)
```
## 2. First cleaning
### 2.1 Keep address, date, name, type & acreage
```{r subset}
biodynamic <- select(biodynamic, name, date, state, address, vineyard, winery, acreage)
glimpse(biodynamic)
```
### 2.2 Clean acreage
```{r clean_acreage}
parse_numbers <- "\\d*,?\\d+\\.?\\d*"
biodynamic$acreage <- str_extract(biodynamic$acreage, parse_numbers)
biodynamic$acreage <- gsub(",", "", biodynamic$acreage)
biodynamic$acreage <- as.numeric(biodynamic$acreage)
head(biodynamic$acreage[!is.na(biodynamic$acreage)])
```
Further visual inspection of the table confirmed that all entries have been correctly identified.
### 2.3 Keep only entries for CA
Sort table for visual inspection
```{r inspect}
biodynamic <- biodynamic[order(biodynamic$name, biodynamic$date), ]
```
Visual inspection of the table confirmed that state and address match for all entries, and that no organization has changed address.
```{r california}
biodynamic <- biodynamic[which(biodynamic$state == "CA"), ]
```
## 3. Fill gaps
We take advantage of the fact that TRUE corresponds to 1 to apply vineyard/winery to all observations for one organization.
```{r fill_gaps, warning=FALSE}
biodynamic <- biodynamic %>%
group_by(name) %>%
mutate(acreage = max(acreage, na.rm = TRUE)) %>%
mutate(winery = max(winery, na.rm = TRUE)) %>%
mutate(vineyard = max(vineyard, na.rm = TRUE))
```
We cause values of -Inf which we will replace with NA.
```{r replace_Inf}
library(naniar)
biodynamic <- biodynamic %>%
replace_with_na(list(vineyard = -Inf, winery = -Inf, acreage = -Inf))
biodynamic$vineyard <- as.logical(biodynamic$vineyard)
biodynamic$winery <- as.logical(biodynamic$winery)
```
## 4. Keep only vineyards/wineries
```{r keep_wine}
biodynamic <- biodynamic[which(biodynamic$vineyard == TRUE | biodynamic$winery == TRUE), ]
head(biodynamic)
```
## 5. Downsample to year
First, we resample to year. Visual inspection shows no conflicts (except spelling) within companies for any of the entries. We keep the last entry for each company-year observation.
```{r fill_years}
library(lubridate)
biodynamic <- biodynamic %>%
mutate(year = year(date)) %>%
group_by(name, year) %>%
mutate(last = last(date)) %>%
filter(date == last) %>%
ungroup() %>%
select(-c(year, last))
head(biodynamic, 5)
```
There are some duplicate entries (same day). The entries differ by nothing,or may have additional entry for "United States" in the address. We therefore apply unique to the dataframe without the address column. We also truncate day & month, and rename date column to year.
```{r unique}
biodynamic <- biodynamic[!duplicated(biodynamic[, -4]), ]
biodynamic$date <- year(biodynamic$date)
biodynamic <- rename(biodynamic, year=date)
head(biodynamic)
```
## 6. Assume continuous existence
```{r fill}
for (n in biodynamic$name){
from = min(biodynamic[biodynamic$name == n, ]$year)
to = max(biodynamic[biodynamic$name == n, ]$year)
for (y in from:to){
if (!(y %in% biodynamic[biodynamic$name == n, ]$year)){
biodynamic <- plyr::rbind.fill(biodynamic, data.frame(name = n, year = y))
}
}
}
```
### 6.1 Backward fill
```{r backward_fill}
biodynamic <- biodynamic[order(biodynamic$name, biodynamic$year, decreasing=TRUE), ]
tail(biodynamic)
biodynamic <- biodynamic %>%
group_by(name) %>%
fill(state, address, vineyard, winery, acreage, .direction = c("down")) %>%
ungroup()
head(biodynamic)
```
## 7. Generate quick map that shows progression (GIF)
### 7.1 Register Google maps API key
```{r register_api}
if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)
key <- readChar('maps_key.txt', file.info('maps_key.txt')$size)
register_google(key = key)
```
### 7.2 Download map of California
```{r download_map}
roadmap <- get_map("California", zoom = 6, source = "google",
maptype = "roadmap") # Using Google Maps.
```
### 7.3 Get coordinates
```{r coordinates, message=FALSE}
biodynamic <- biodynamic %>%
mutate_geocode(location = address)
```
### 7.4 Generate map 1
```{r map1}
library(gganimate)
biodynamic_temp <- biodynamic
biodynamic_temp$year <- as.numeric(biodynamic_temp$year)
map1 <- ggmap(roadmap, base_layer = ggplot(biodynamic_temp, aes(lon, lat))) +
geom_point(aes(lon, lat), size = 4, alpha = 0.3) +
labs(title = 'Year: {frame + 2013}', x = '', y = '') +
theme(text = element_text(size=20),
axis.ticks = element_blank(),
axis.text = element_blank()) +
transition_manual(year)
map1
anim_save("map1.gif", map1, height = 700, width = 700)
```
### 7.5 Create factor variable for organization type
```{r factor}
biodynamic$type <- NA
biodynamic$type[biodynamic$vineyard] <- "vineyard"
biodynamic$type[biodynamic$winery] <- "winery"
biodynamic$type[biodynamic$winery & biodynamic$vineyard] <- "vineyard & winery"
biodynamic$type <- factor(biodynamic$type, ordered = TRUE, levels = c("vineyard", "winery", "vineyard & winery"))
```
### 7.6 Generate map2
```{r map2}
library(gganimate)
biodynamic_temp <- biodynamic
biodynamic_temp$year <- as.numeric(biodynamic_temp$year)
map2 <- ggmap(roadmap, base_layer = ggplot(biodynamic_temp, aes(lon, lat))) +
geom_point(aes(lon, lat), size = 4, alpha = 0.3) +
facet_wrap(~type) +
labs(title = 'Year: {frame + 2013}', x = '', y = '') +
theme(text = element_text(size=20),
axis.ticks = element_blank(),
axis.text = element_blank()) +
transition_manual(year)
map2
anim_save("map2.gif", map2, height = 700, width = 1950)
```