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 github using devtools.
# install.package("devtools") #In case you have not installed it. devtools::install_github("wenlong-liu/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 = "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
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 of plotting us maps of fertilizer.
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
(Under development on July 10, 2018)
If you have any problems or questions, feel free to open an issue here.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.