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:

library(zoo)
## Warning: package 'zoo' was built under R version 3.4.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
ukacc <- data.frame(Seatbelts, date = as.Date(as.yearmon((time(Seatbelts)))))

# Find the max and min values to set the y axis
min_ylim <- min(ukacc$drivers, ukacc$front, ukacc$rear)
max_ylim <- max(ukacc$drivers, ukacc$front, ukacc$rear)

if(min(min_ylim,0) == 0){
 min_ylim <- 0 
}

max_ylim <- max_ylim + 100

Including Plots

You can also embed plots, for example:

# 3 plot statements
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.3
p1 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$drivers),  size = 1) + geom_line() + ylim(c(min_ylim,max_ylim)) 
p2 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$front),  size = 1) + geom_line() + ylim(c(min_ylim,max_ylim))
p3 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$rear), size = 1) + geom_line() + ylim(c(min_ylim,max_ylim)) 


library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), ggplotGrob(p3), size = "last"))

limit_vector <- c(min_ylim,max_ylim)

break_vector <- seq(min_ylim,max_ylim,300)
# 3 plot statements

p1 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$drivers)) + geom_line() + 
  scale_y_continuous(limits = limit_vector,breaks= break_vector)
 
p2 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$front)) + geom_line() +
  scale_y_continuous(limits = limit_vector,breaks= break_vector)

p3 <- ggplot(ukacc,aes(x = ukacc$date, y = ukacc$rear)) + geom_line() +
  scale_y_continuous(limits = limit_vector,breaks= break_vector)
  
grid.newpage()
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), ggplotGrob(p3), size = "last"))