forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
40 lines (31 loc) · 1.72 KB
/
plot4.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
library(sqldf)
# This code processes the "Individual household electric power consumption Data Set"
# from the UC Irvine Machine Learning Repository.
# Remote URL where the data lives.
datasetURL = "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
dataFile = "household_power_consumption.txt"
if (!file.exists(dataFile)) {
message("Retrieving UCI data from '", datasetURL, "'")
f <- tempfile()
download.file(datasetURL, f, method="curl")
unzip(f)
}
# This performs well enough on the 2 machines I've tried it on
power2007 = read.csv.sql(dataFile,
sql="select * from file where Date = '1/2/2007' or Date = '2/2/2007'",
header = TRUE, row.names = FALSE, sep = ";")
power2007$DateTime = as.POSIXlt(paste(power2007$Date, power2007$Time), format='%d/%m/%Y %H:%M:%S')
png('plot4.png', width = 480, height = 480)
par(mfrow=c(2,2))
# Plot 1
plot.default(x = power2007$DateTime, y = power2007$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power")
# Plot 2
plot.default(x = power2007$DateTime, y = power2007$Voltage, type = "l", xlab = "datetime", ylab = "Voltage")
# Plot 3
plot(x = power2007$DateTime, y = power2007$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering")
lines(x = power2007$DateTime, y = power2007$Sub_metering_2, type = "l", col = "red")
lines(x = power2007$DateTime, y = power2007$Sub_metering_3, type = "l", col = "blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, bty = "n", col=c("black", "red", "blue"))
# Plot 4
plot.default(x = power2007$DateTime, y = power2007$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power")
graphics.off()