-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
135 lines (103 loc) · 4.57 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
---
output: rmarkdown::github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
# ggfertilizer
![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggfertilizer)
[![Travis-CI Build Status](https://travis-ci.org/wenlong-liu/ggfertilizer.svg?branch=master)](https://travis-ci.org/wenlong-liu/ggfertilizer)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/wenlong-liu/ggfertilizer?branch=master&svg=true)](https://ci.appveyor.com/project/wenlong-liu/ggfertilizer)
[![](https://cranlogs.r-pkg.org/badges/ggfertilizer)](https://cran.r-project.org/package=ggfertilizer)
[![metacran downloads](http://cranlogs.r-pkg.org/badges/grand-total/ggfertilizer?color=ff69b4)](https://cran.r-project.org/package=ggfertilizer)
## Retrieve, Summarize and Visualize the Fertilizer Data in USA
Provides a user-friendly API to further dig in County-Level Fertilizer data in USA, provided by USGS.
## Installation
This package can be installed via github using devtools.
```{r, eval=FALSE}
# install.package("devtools") #In case you have not installed it.
devtools::install_github("wenlong-liu/ggfertilizer")
```
## Quick Start
### Import data and libraries
```{r data, message=FALSE}
require(ggfertilizer)
require(dplyr)
require(ggplot2)
data("us_fertilizer_county")
```
### Retrieve fertilizer data
```{r}
Year <- 2008
Nutrient <- "N"
Input_Type <- "fertilizer"
# retrieve data.
plot_data <- get_data(data = us_fertilizer_county, years = Year, nutrient = Nutrient,
input_type = Input_Type, combine_state_county = TRUE)
head(plot_data)
```
### Summarize and plot data.
#### Example 1: Find out the top 10 counties with most nitrogen appliation in 2008.
```{r}
# plot the top 10 nitrogen application in year 2008.
plot <- plot_data %>%
top_n(10, Quantity) %>%
ggplot(aes(x=reorder(County, Quantity), Quantity, fill = Quantity))+
scale_fill_gradient(low = "yellow", high = "brown")+
geom_col()+
ggtitle(paste("Top 10 counties with most N fertilizer application in the year of", Year)) +
scale_y_continuous(name = "Nitrogen from commecial fertilization (kg)")+
scale_x_discrete(name = "Counties")+
coord_flip()+
theme_bw()
plot
```
### Examples 2: Visualize the fertilizer data in US maps.
```{r}
Year = 2001
Nutrient = "N"
Farm_Type = "farm"
Input_Type = "fertilizer"
level = "county"
# draw the map
us_plot <- map_us_fertilizer(data = us_fertilizer_county, Year = Year, Nutrient = Nutrient,
Farm_Type = Farm_Type, Input_Type = Input_Type,
viridis_palette = "inferno", level = level)
us_plot
```
As the maps are actually ggplot2 objects, all the common API for ggplot2 can be used here. We can also add a title for the map to make it more informative.
```{r}
us_plot +
ggtitle(paste(Nutrient, " from ", Input_Type, " input to ", Farm_Type, " in the year of ",Year,
" at ", level, " level",sep = ""))
```
For more details about mapping fertilizer data, please see this [vignettes of plotting us maps of fertilizer.](https://wenlong-liu.github.io/ggfertilizer/articles/US_maps.html)
### Example 3: Visualize the fertilizer data for certain states or counties.
```{r}
Year = 2011
Nutrient = "P"
Farm_Type = "nonfarm"
Input_Type = "fertilizer"
level = "county"
State = c("NC", "SC")
# draw the map
state_plot <- map_us_fertilizer(data = us_fertilizer_county, Year = Year, Nutrient = Nutrient,
Farm_Type = Farm_Type, Input_Type = Input_Type, State = State,
viridis_palette = "inferno", level = level) +
ggtitle(paste(Nutrient, " from ", Input_Type, " input to ", Farm_Type, " in the year of ",Year,
" at ", level, " level for Carolinas",sep = ""))
state_plot
```
For more details about mapping fertilizer data, please see this [vignettes of plotting state maps](https://wenlong-liu.github.io/ggfertilizer/articles/State_fertilizer_maps.html)
### Generate summaries plots.
(Under development on July 10, 2018)
## Comments and Questions
If you have any problems or questions, feel free to open an issue [here](https://github.com/wenlong-liu/ggfertilizer/issues).
## Lisence
[GPL](https://github.com/wenlong-liu/ggfertilizer/blob/master/LICENSE)
## Code of conduct
Please note that this project is released with a [Contributor Code of Conduct](https://github.com/wenlong-liu/ggfertilizer/blob/master/CONDUCT.md). By participating in this project you agree to abide by its terms.