knitr::opts_chunk$set(echo = TRUE)
First,we're going to load in the libraries we will use with my functions:
install.packages("tidyverse") install.packages("ggplot2") library(tidyverse) library(ggplot2)
Let's get my R package from github:
devtools::install_github("awatt008/R_Package_Wattler") library(projects)
Some may need the surveys.csv data, so use this download/install code to get that:
download.file(url = "https://raw.githubusercontent.com/awatt008/R_Package_Wattler/master/data/surveys.csv", destfile = "/cloud/project/data/surveys.csv")
Now, we read the surveys data with tidyverse
surveys <- read_csv("/cloud/project/data/surveys.csv")
Let's take a look at my functions first, then we'll see how they're used with tidyverse:
boxplot_weight_species <- function(., species_id,weight){ if(!is.data.frame(surveys)){ return("This is not a dataframe") } plt <- ggplot(., mapping = aes(x = weight, y = species_id)) + geom_boxplot() return(plt) }
The output of the boxplot_weight_species function plus the piped code will return a boxplot with weight and species selected and its corresponding data grouped from the surveys data frame.
plotted_weight_species<-surveys%>% filter(!is.na(species_id))%>% filter(!is.na(weight))%>% select(species_id, weight) %>% boxplot_weight_species(species_id = species_id, weight = weight)
Next, we'll look at a function that removes NA's from surveys that only requires inputting the database name plus the function and weight column.
no_na_weight <- function(., weight){ if(any(!is.na(surveys$weight))) scrub_weight_na <- surveys%>% filter(!is.na(weight)) return(scrub_weight_na) }
So, this short line of code below will return surveys data without the NAs:
surveys%>% no_na_weight(weight = weight)
And now, a function that creates a line of regression for surveys that works once we run a pipe that filters NA's, selects hindfoot and weight, and groups their corresponding data:
hindfoot_weight_regression <- function(., weight, hindfoot_length){ if(!is.data.frame(surveys)){ return("This is not a dataframe") } plt_regression <- ggplot(., aes(x = weight, y = hindfoot_length)) + geom_point(size = .25) + geom_smooth(method = "lm", color = "blue", size = .5, fill = "black") return(plt_regression) }
And here's that code:
plot_of_regression<- surveys%>% filter(!is.na(weight))%>% filter(!is.na(hindfoot_length))%>% select(weight,hindfoot_length)%>% group_by(weight,hindfoot_length)%>% hindfoot_weight_regression(weight = weight, hindfoot_length = hindfoot_length)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.