forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
44 lines (39 loc) · 2.16 KB
/
plot4.R
File metadata and controls
44 lines (39 loc) · 2.16 KB
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
44
# plot4.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)
}
plot4 <- 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 = "plot4.png", width = 480, height = 480)
par (mfrow = c(2,2))
plot (hpc$DateTime, hpc$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power")
plot (hpc$DateTime, hpc$Voltage, type = "l", xlab = "datetime", ylab = "Voltage")
with (hpc, plot (DateTime, Sub_metering_1, type = "l", ylab = "Energy sub metering", xlab = ""))
with (hpc, points (DateTime, Sub_metering_2, type = "l", col = "red"))
with (hpc, points (DateTime, Sub_metering_3, type = "l", col = "blue"))
legend ("topright", lty = c(1, 1, 1), col = c("black", "red", "blue"),
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), bty = "n")
plot (hpc$DateTime, hpc$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power")
dev.off()
}