This repository was archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexample_harvest_animation.Rmd
93 lines (88 loc) · 3.81 KB
/
example_harvest_animation.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
---
title: "Harvest of Field"
author: "Emi Tanaka"
date: "22 November 2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(gganimate)
library(ggimage)
# make data set for movement of the tractor
harvest_df <- data.frame(
x=c(-0.375 + 0.75*(0:15), rep(-0.375 + 0.75*15, 3), -0.375 + 0.75*(15:0), rep(-0.375+0, 3),
-0.375 + 0.75*(0:15), rep(-0.375 + 0.75*15, 3), -0.375 + 0.75*(15:0), rep(-0.375+0, 3),
-0.375 + 0.75*(0:15), rep(-0.375 + 0.75*15, 3), -0.375 + 0.75*(15:0), rep(-0.375+0, 3),
-0.375 + 0.75*(0:15), rep(-0.375 + 0.75*15, 3), -0.375 + 0.75*(15:0)
),
y=6 * c(rep(-1, 16), 0:2, rep(3, 16), 3:5,
rep(6, 16), 6:8, rep(9, 16), 10:12,
rep(12,16), 12:14, rep(15, 16), 16:18,
rep(18,16), 18:20, rep(21, 16)),
image=c(rep("images/tractor_right.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_left.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_right.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_left.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_right.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_left.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_right.png", 16),
rep("images/tractor_vertical.png", 3),
rep("images/tractor_left.png", 16)),
xpos=c(0:15, rep(15,3), 15:0, rep(0, 3),
0:15, rep(15,3), 15:0, rep(0, 3),
0:15, rep(15,3), 15:0, rep(0, 3),
0:15, rep(15,3), 15:0),
ypos=c(rep(-1, 16), 0:2, rep(3, 16), 3:5,
rep(6, 16), 6:8, rep(9, 16), 10:12,
rep(12,16), 12:14, rep(15, 16), 16:18,
rep(18,16), 18:20, rep(21, 16))
)
harvest_df$time <- 1:nrow(harvest_df)
# make the field with change of color as it gets harvested
ggdat <- expand.grid(x=0.75 * c(0:14),
y=6 * c(0:21),
time=1:nrow(harvest_df),
fill="#009900") %>% mutate(fill=as.character(fill)) # green
ggdat$xpos <- ggdat$x/0.75
ggdat$ypos <- ggdat$y/6
for(atime in 1:nrow(harvest_df)) {
ggdat <- ggdat %>% mutate(
fill=case_when(
ypos==0 & xpos < (atime - 1) & time==atime & time %in% 2:16 ~ "#8B4513",
ypos==0 & xpos %in% 0:14 & time > 16 ~ "#8B4513",
ypos %in% 1:3 & (14 - xpos) <= atime%%21 & time==atime & time %in% 21:35 ~ "#8B4513",
ypos %in% 1:3 & xpos %in% 0:14 & time > 35 ~ "#8B4513",
ypos %in% 4:6 & xpos <= atime%%39 & time==atime & time %in% 39:54 ~ "#8B4513",
ypos %in% 4:6 & xpos %in% 0:14 & time > 54 ~ "#8B4513",
ypos %in% 7:9 & (14 - xpos) <= atime%%58 & time==atime & time %in% 58:73 ~ "#8B4513",
ypos %in% 7:9 & xpos %in% 0:14 & time > 73 ~ "#8B4513",
ypos %in% 10:12 & xpos <= atime%%77 & time==atime & time %in% 77:92 ~ "#8B4513",
ypos %in% 10:12 & xpos %in% 0:14 & time > 92 ~ "#8B4513",
ypos %in% 13:15 & (14 - xpos) <= atime%%96 & time==atime & time %in% 96:111 ~ "#8B4513",
ypos %in% 13:15 & xpos %in% 0:14 & time > 111 ~ "#8B4513",
ypos %in% 16:18 & xpos <= atime%%115 & time==atime & time %in% 115:130 ~ "#8B4513",
ypos %in% 16:18 & xpos %in% 0:14 & time > 130 ~ "#8B4513",
ypos %in% 19:21 & (14 - xpos) <= atime%%134 & time==atime & time %in% 134:149 ~ "#8B4513",
ypos %in% 19:21 & xpos %in% 0:14 & time > 149 ~ "#8B4513",
TRUE ~ fill
))
}
```
```{r, cache=T}
ga <- ggplot(ggdat, aes(x, y)) +
geom_tile(aes(fill=I(fill)), height=6, width=0.75, colour="white", size=1.2) +
geom_image(data=harvest_df,
aes(x, y, image=image), size=0.09) +
theme_void() + transition_time(time) +
ease_aes('linear')
animate(ga, width=400)
```