R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(broom)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(scales)

##Analyze the relationship between brain weight and body weight across animals #Data slide

data <- ggplot2::msleep
clean_data <- subset(data, !is.na(brainwt) & !is.na(bodywt))
head(clean_data[, c("name", "bodywt", "brainwt")])

#ggplot1

ggplot(clean_data, aes(x = bodywt, y = brainwt)) +
  geom_point(color = "green") +
  labs(title = "Brain vs Body Weight in Animals",
       x = "Body Weight (kg)",
       y = "Brain Weight (kg)")

#ggplot2

model <- lm(brainwt ~ bodywt, data = clean_data)
ggplot(clean_data, aes(x = bodywt, y = brainwt)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "lightpink") +
labs(title = "Linear Regression: Brain vs Body Weight",
x = "Body Weight (kg)", y = "Brain Weight (kg)")
## `geom_smooth()` using formula = 'y ~ x'

#plotly

plot_ly(clean_data, x = ~bodywt, y = ~brainwt, z = ~sleep_total,
type = "scatter3d", mode = "markers",
marker = list(color = 'maroon', size = 4)) %>%
layout(scene = list(xaxis = list(title = 'Body Weight (kg)'),
yaxis = list(title = 'Brain Weight (kg)'),
zaxis = list(title = 'Sleep (hrs)')))

#Math using Latex - 1 \[t = r \sqrt{\frac{n-2}{1 - r^2}}\] #Math using Latex - 2 \[ \text{Brain Weight} = \beta_0 + \beta_1(\text{Body Weight}) \] # R code slide

ggplot(clean_data, aes(x = bodywt, y = brainwt)) +
  geom_point() +
  labs(title = "Brain vs Body Weight",
       x = "Body Weight (kg)",
       y = "Brain Weight (kg)")

#ggplot - Sleep v Body Weight

ggplot(clean_data, aes(x = bodywt, y = sleep_total)) +
  geom_point(color = "blue") +
  labs(title = "Sleep Hours vs Body Weight in Animals",
       x = "Body Weight (kg)",
       y = "Total Sleep (hours per day)")