-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
155 lines (115 loc) · 4.49 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# cropdatape
`cropdatape` provides peruvian agricultural production data from the Agriculture Minestry of Peru (MINAGRI). The first version includes 6 crops: rice, quinoa, potato, sweet potato, tomato and wheat; all of them across 24 departments. Initially, in excel files which has been transformed and assembled using tidy data principles, i.e. each variable is in a column, each observation is a row and each value is in a cell. The variables variables are sowing and harvest area per crop, yield, production and price per plot, every one year, from 2004 to 2014.
## Installation
You can install `cropdatape` directly from `CRAN`:
```{r, eval=FALSE}
install.packages("cropdatape")
```
Or, you can install from `GitHub`:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("omarbenites/cropdatape")
```
The `cropdatape` data frame include 9 variables,
| variable | meaning | units |
|:------------|:-------------------------|----------|
| crop | crop | - |
| department | deparment or region | - |
| year | year | - |
| month | month | - |
| sowa | sowing area | ha |
| harva | harvested area | ha |
| production | production | t |
| yield | yield | kg/ha |
| pricePlot | price per plot | s/kg |
### Usage
#### Example 1: Filter, grouped and summarize cropdatape data
In this example, we will explore the cropdatape dataset, using three (dplyr) functionlities: `filter`, `group` and `summarize`.
1. `filter` crop by `sweet potato`.
2. `group_by` department column.
3. `summarise` by mean of the sweetpotato yield.
`cropdatape` package:
```{r, message = FALSE, warning = FALSE}
#Load cropdatape package
library(cropdatape)
#Load dplyr package to filter and select information
library(dplyr)
cropdatape %>%
filter(crop == "sweet potato") %>%
group_by(department, year) %>%
summarise(yieldMean = mean(yield, na.rm = TRUE))
```
#### Example 2: Plot graphics with ggplot using cropdatape data
This second example we will explore the behaviour of the `yield` varible grouped by `crop`, from 2004 till 2014. The `crop` variable involves 6 crops: potato, quinoa, rice, sweet potato and wheat.
```{r example, echo=TRUE, warning=FALSE}
library(cropdatape)
library(ggplot2)
ggplot(cropdatape, aes(x = crop, y = yield)) +
geom_boxplot(outlier.colour = "hotpink") +
geom_jitter(position = position_jitter(width = 0.1, height = 0), alpha = 1/4)
```
#### Example 3: Animations with gganimate
To begin with, install the following packages from Github:
```{r,eval=FALSE}
#Install first devtools package
#install.packages("devtools")
library(devtools)
install_github("thomasp85/gganimate")
install_github("thomasp85/transformr")
install_github("thomasp85/tweenr")
```
Then, we will filter all the information related to sweetpotato
```{r, message=FALSE, warning=FALSE}
library(cropdatape)
library(dplyr)
sp <- cropdatape %>%
filter(crop == "quinoa", department == "Puno") %>%
group_by(department, year) %>%
summarise(sowaMean = mean(sowa,na.rm = TRUE),
harvaMean = mean(harva, na.rm = TRUE),
yieldMean = mean(yield, na.rm = TRUE))
```
Plotting and animating the scatter graph `years` vs `yieldMean`
```{r, cache=TRUE, warning=FALSE}
library(gganimate)
library(ggplot2)
library(transformr)
sp$year <- as.integer(sp$year)
yearlbl<- sp$year
ggplot(sp, aes(year, yieldMean)) +
geom_point(size= 1.5)+
scale_x_continuous(breaks = yearlbl)+
labs(title = 'Year: {frame_time}', x = 'Year', y = 'Yield') +
transition_time(year) +
ease_aes('linear')
```
Install and `emojifonts` package:
```{r, message=FALSE, warning=FALSE}
devtools::install_github("dill/emoGG")
library(emoGG)
```
Let the animation begins,
```{r, cache=TRUE, warning=FALSE}
library(gganimate)
library(ggplot2)
library(transformr)
sp$year <- as.integer(sp$year)
yearlbl<- sp$year
ggplot(sp, aes(year, yieldMean)) +
scale_x_continuous(breaks = yearlbl)+
geom_emoji(emoji="1f360")+
labs(title = 'Year: {frame_time}', x = 'Year', y = 'Yield') +
transition_time(year) +
ease_aes('linear')
```