This is an assignment on creating a report with R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. The assignment makes use of data from a personal activity monitoring device.This device collects data at 5 minute intervals through out the day.
For this assignment I created this plot with [library(data.table) - library(ggplot2) - library(grid)] a panel plot in a plot of the activity trends to show the distinct trends in week days vs weekends which is blurred in the daily plot.
panel plot in plot
With Raw Data(i)What is mean total number of steps taken per day? (ii)What is the average daily activity pattern? (iii) Then Create & Use Tidy Data that is equal to the original dataset but with the missing data filled in. (iv) What is the impact of imputing missing data on the estimates of the total daily number of steps? (v) Are there differences in activity patterns between weekdays and weekends?
library(data.table)
library(ggplot2)
#Show any code that is needed to
#1. Load the data (i.e. `read.csv()`) from the working directory.
csvfile <- "activity.csv"
if(!file.exists("activity.csv")){
download.file(
"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",
destfile="repdataactivity.zip")
unzip(zipfile="repdataactivity.zip",exdir=getwd()) }
step<-fread("activity.csv",sep=",",header=T)
step$date<-as.Date.character(step$date)
#For this part of the assignment, you can ignore the missing values
#in the dataset.
#First Draft:
#1. Make a histogram of the total number of steps taken each day
############################HISTOGRAM_PLOT################################
library(data.table)
library(ggplot2)
totsteps<-step[,sum(steps,na.rm=T),by=date]
names(totsteps)[2]<-"steps"
ggplot(data=totsteps,aes(steps)) +
geom_histogram(binwidth=21194/30) +
labs(x="Steps per Day", y="Frequency",
title="Total number of steps taken per day") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_vline(aes(xintercept=median(totsteps$steps),color="Median"), lwd=1.15) +
geom_vline(aes(xintercept=mean(totsteps$steps),color="Mean") , lwd=1.15)
ggsave("plot1.png")
## Saving 7 x 5 in image
meantotsteps <- mean(totsteps$steps)
mediantotsteps <- median(totsteps$steps)
library(data.table)
library(ggplot2)
avgsteps<-step[,mean(steps,na.rm=T),by=interval]
names(avgsteps)[2]<-"steps"
ggplot(data=avgsteps,aes(x=interval,y=steps))+geom_line() +
labs(x="5 Minute Intervals", y="Average Steps",
title="Average daily activity pattern") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,2400,200)) +
geom_vline(aes(xintercept=avgsteps$interval[which.max(avgsteps$steps)])
, col="red",lwd=1.15 )+
geom_line(size = 1.25)
ggsave("plot2.png")
## Saving 7 x 5 in image
summary(avgsteps)
## interval steps
## Min. : 0.0 Min. : 0.000
## 1st Qu.: 588.8 1st Qu.: 2.486
## Median :1177.5 Median : 34.113
## Mean :1177.5 Mean : 37.383
## 3rd Qu.:1766.2 3rd Qu.: 52.835
## Max. :2355.0 Max. :206.170
maxsteps <- avgsteps[steps==max(avgsteps$steps),]
minsteps <- avgsteps[steps==min(avgsteps$steps),]
#Note that there are a number of days/intervals where there are
#missing values (coded as `NA`). The presence of missing days may
#introduce bias into some calculations or summaries of the data.
# 1. Calculate and report the total number of missing values in the
# dataset (i.e. the total number of rows with `NA`s)
totmissing <- is.na(step$steps)
# How many missing
Table1 <-table(totmissing)
#totmissing
#FALSE TRUE
#15264 2304
# 2. Devise a strategy for filling in all of the missing values in
# the dataset. The strategy does not need to be sophisticated.
# For example, you could use the mean/median for that day, or
# the mean for that 5-minute interval, etc.
#Input Strategy:
# (i) Create a copy of the original data table step in variable imputedstep
inputedstep<-step
#(ii)) Create a logical vector for the locations of the missing values
missingvec<-!complete.cases(inputedstep)
#(iii) Create a vector of the averages, dim(steps) = 61)
avgvec<-rep(avgsteps$steps,61)
#(iv) Convert the NA values to 0, in order to be summable.
inputedstep$steps[is.na(inputedstep$steps)]<-0
#(v) Add the product of the two vectors to the steps variable
inputedstep$steps<-inputedstep$steps+avgvec*missingvec
This is a table of missing values = TRUE, if NA in cell: 15264, 2304
# 3. Create a new dataset that is equal to the original dataset but
# with the missing data filled in.
inputedtotsteps<-inputedstep[,sum(steps,na.rm=T),by=date]
names(inputedtotsteps)[2]<-"steps"
#4. Make a histogram of the total number of steps taken each day
# and Calculate and report the **mean** and **median** total
# number of steps taken per day. Do these values differ from the
# estimates from the first part of the assignment? What is the
# impact of imputing missing data on the estimates of the total
# daily number of steps?
ggplot(data=inputedtotsteps,aes(steps)) +
geom_histogram(binwidth=21194/30) +
labs(x="Steps per day", y="Frequency",
title="Normalised Histogram of Steps per Day") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_vline(aes(xintercept=median(inputedtotsteps$steps),color="Median"), lwd=2.5) +
geom_vline(aes(xintercept=mean(inputedtotsteps$steps),color="Mean"), lwd=1.2)
ggsave("plot3.png")
## Saving 7 x 5 in image
# For this part the `weekdays()` function may be of some help here.
# Use the dataset with the filled-in missing values for this part.
# 1. Create a new factor variable in the dataset with two levels --
# "weekday" and "weekend" indicating whether a given date is a
# weekday or weekend day.
# 2. Make a panel plot containing a time series plot (i.e. `type =
# "l"`) of the 5-minute interval (x-axis) and the average number
# of steps taken, averaged across all weekday days or weekend
# days (y-axis). The plot should look something like the
# following, which was created using **simulated data**:
# 
step$daytype<-as.factor(weekdays(step$date) %in% c("Saturday","Sunday"))
levels(step$daytype)<-c("Weekday","Weekend")
avgwksteps<-step[ ,mean(steps, na.rm=T),by=list(daytype, interval)]
names(avgwksteps)[3]<-"steps"
# don't do facet_wrap(steps~daytype)!!
ggplot(data=avgwksteps, aes(x=interval,y=steps)) + geom_line() +
facet_wrap(.~daytype) +
labs(x="5 Minute Intervals", y="Average Steps",
title="Differences in activity patterns",
subtitle= "Weekdays vs Weekends") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,2500,500))
ggsave("plot4.png")
## Saving 7 x 5 in image
## This plot is is different from the example as I used the activity
# monitor data, and the above plot was made with the ggplot2 system.
#Set Environment:
library(data.table)
library(ggplot2)
library(grid)
# Get Data & Datsets/
csvfile <- "activity.csv"
if(!file.exists("activity.csv")){
download.file(
"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",
destfile="repdataactivity.zip")
unzip(zipfile="repdataactivity.zip",exdir=getwd()) }
step<-fread("activity.csv",sep=",",header=T)
step$date<-as.Date.character(step$date)
# reset theme
theme_set(theme_bw())
avgsteps<-step[,mean(steps,na.rm=T),by=interval]
names(avgsteps)[2]<-"steps"
step$daytype<-as.factor(weekdays(step$date) %in% c("Saturday","Sunday"))
levels(step$daytype)<-c("Weekday","Weekend")
avgwksteps<-step[ ,mean(steps, na.rm=T),by=list(daytype, interval)]
names(avgwksteps)[3]<-"steps"
# Inset figures:
theme_set(theme_bw() +
theme(
axis.line = element_line(size=0.25),
panel.background = element_rect(fill=NA,size=rel(20)),
panel.grid.minor = element_line(colour = NA),
axis.text = element_text(size=8),
axis.title = element_text(size=12)
)
)
big_plot <- ggplot(data=avgsteps,aes(x=interval,y=steps))+geom_line() +
labs(x="5 Minute Intervals", y="Average Steps",
title="Average daily activity pattern") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,2400,200)) +
geom_vline(aes(xintercept=avgsteps$interval[which.max(avgsteps$steps)])
, col="red",lwd=1.15 )+
geom_line(size = 1.25)
big_plot
small_plot <- ggplot(data=avgwksteps, aes(x=interval,y=steps)) + geom_line() +
facet_wrap(.~daytype) +
labs(x="5 Minute Intervals", y="Average Steps",
title="Differences in activity patterns",
subtitle= "Weekdays vs Weekends") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,2500,1000)) +
theme(axis.title=element_blank()) +
scale_y_continuous(expand=c(0,0))
small_plot
# Where to put the smaller plot:
library(grid)
vp <- viewport(width = 0.35, height = 0.35, x = 0.75, y = 0.75)
# width, height, x-position, y-position of the smaller plot
png("C:/Users/angul/OneDrive/R/ReproducibleResearch/figure/inset_plot.png")
print(big_plot)
print(small_plot, vp = vp)
dev.off()
## png
## 2
This is my main plot: an insert plot of activity pattern, the daytype by weekday vs week end inserted in the daily activity pattern.
Insert Plot
The variables included in this dataset are:
steps: Number of steps taking in a 5-minute interval (missing values are coded as NA)
date: The date on which the measurement was taken in YYYY-MM-DD format
interval: Identifier for the 5-minute interval in which measurement was taken
The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
Note that the echo = TRUE was added to the code as a global parameter to allow printing of the R code that generated the plots, if you’d like a report without the R code please edit to echo = FALSE in line 8. For more details on using R Markdown see http://rmarkdown.rstudio.com.
The aim of this assignment is to create and share a plot like this: