forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
executable file
·42 lines (38 loc) · 1.48 KB
/
plot1.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
#Author: Fred Rahmanian
#
#call createPlot1() 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)
}
createPlot1 <- function () {
setWorkingDir()
df <- loadData()
png(filename = "figure/plot1.png",
width = 480, height = 480, units = "px")
hist(df$Global_active_power, xlab='Global Active Power (kilowatts)', main='Global Active Power', col='red')
dev.off()
}