ggfertilizer---
title: "Get started with ggfertilizer"
author: "Wenlong Liu"
date: "r Sys.Date()
"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Get started with ggfertilizer}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Provides a user-friendly API to further dig in County-Level Fertilizer data in USA, provided by USGS.
This package can be installed via CRAN.
# Not available yet. install.packages("ggfertilizer")
require(ggfertilizer) require(dplyr) require(ggplot2) data("us_fertilizer_county")
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)
# 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 = "blue", high = "darkblue")+ 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
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.
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
(under development on July 10, 2018)
If you have any problems or questions, feel free to open an issue here.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.