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.

Plot 1: Create a histogram categorized by Month with qplot

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.1     v purrr   0.3.2
## v tibble  2.0.1     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.5.3
## Warning: package 'tibble' was built under R version 3.5.2
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'stringr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts ---------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
p1<-qplot(data=airquality,Temp,fill=Month,geom="histogram",bins=20)
p1

# Plot 2: Make a histogram using ggplot (instead of qplot)

p2<-airquality %>%
  ggplot(aes(x=Temp,fill=Month)) +
  geom_histogram(position="identity",alpha=0.7,binwidth=5,color="black")+
  scale_fill_discrete(name="Month",labels=c("May", "June","July", "August", "September"))
p2

Plot 3: Create side-by-side boxplots categorized by Month

p3<-airquality %>%
  ggplot(aes(Month,Temp,fill=Month)) + 
  ggtitle("Temperatures") +
  xlab("Months") +
  ylab("Frequency") +
  geom_boxplot() +
  scale_fill_brewer(palette="Blues",name="Month",labels=c("May","June","July","August","September"))
p3 
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?

Plot 4: Make the same side-by-side boxplots, but in grey-scale

p4<-airquality %>%
  ggplot(aes(Month,Temp,fill=Month)) + 
  ggtitle("Temperatures") +
  xlab("Temperatures") +
  ylab("Frequency") +
  geom_boxplot()+
  scale_fill_grey(name="Month",labels=c("May","June","July","August","September"))
p4
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?