forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
executable file
·45 lines (40 loc) · 1.76 KB
/
plot3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#Author: Fred Rahmanian
#
#call createPlots() to create all four plots for this project
#Assumptions:
# 1) You must be running this app in the ~/Projects/data-sci-spec/ExData_Plotting1. Change setWorkingDir if
# you want to run it in another dirs
# 2) THere is a child dir called figure in the dir where you run this
# 3) data file is in data dir (under main project dir)
#
#
setWorkingDir <- function() {
if (Sys.info()['sysname'] == 'Darwin') {
setwd("~/Projects/data-sci-spec/ExData_Plotting1")
}else {
setwd("D:/Projects/data-sci-spec/ExData_Plotting1")
}
}
loadData <- function() {
#create a method to format the date during load since it is not in yyyy-mm-dd
setClass("mdyDate")
setAs("character","mdyDate", function(from) as.Date(from, format="%d/%m/%Y") )
df <- read.csv(file='data/household_power_consumption.txt', header=T, sep=';', na.strings=c("?"),
colClasses=c("mdyDate", "character", rep("numeric", 7)))
#subset the data only for the days we need for this project
df <- subset(df, Date >= '2007-02-01' & Date <= '2007-02-02')
#create date time column. Need this for plot2
df$dateTime <- strptime(paste(df$Date, df$Time), format="%Y-%m-%d %H:%M:%S")
return(df)
}
createPlot3 <- function () {
setWorkingDir()
df <- loadData()
png(filename = "figure/plot3.png",
width = 480, height = 480, units = "px")
plot(df$dateTime,df$Sub_metering_1, ylab='Energy sub metering', xlab='',type='l')
lines(df$dateTime,df$Sub_metering_2, ylab='Energy sub metering', xlab='',type='l', col='red')
lines(df$dateTime,df$Sub_metering_3, ylab='Energy sub metering', xlab='',type='l', col='blue')
legend("topright", col=c('black', 'red', 'blue'), legend=c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), lty=1)
dev.off()
}