forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
36 lines (31 loc) · 1.52 KB
/
plot2.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
# plot2.R
library (dplyr)
# globals
gDownloadedToday = TRUE
gFileName = "household_power_consumption.txt"
loadData <- function () {
# estimate of file size: 2,075,259 rows, 9 columns, or 18677331 entries
# at 8 bytes per, 149418648 bytes, or roughly 150 MB; plenty of room
# after loading, object.size (hpcRawData) returns 149604992
retVal = !gDownloadedToday
zipFileName = "hpc.zip"
if (!file.exists(gFileName)) {
datasetZipURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(datasetZipURL, zipFileName, "curl")
unzip (zipFileName)
retVal = gDownloadedToday
}
return (retVal)
}
plot2 <- function () {
if (loadData () == gDownloadedToday)
dateDownloaded <<- date ()
hpcRawData <- read.table (gFileName, header = TRUE, sep=";", na.strings = "?", stringsAsFactors = FALSE)
# Notice that the days in the dataset are Day, Month, Year, NOT Month, Day Year as is usual in the US
hpcI <- subset (hpcRawData, Date == "1/2/2007" | Date == "2/2/2007")
hpc <<- mutate (hpcI, DateTime = as.POSIXct (strptime (paste (hpcI$Date, hpcI$Time), format = "%d/%m/%Y %H:%M:%S")))
rm (hpcRawData)
png (filename = "plot2.png", width = 480, height = 480)
plot (hpc$DateTime, hpc$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power (kilowatts)")
dev.off()
}