forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
28 lines (22 loc) · 1.25 KB
/
Copy pathplot3.R
File metadata and controls
28 lines (22 loc) · 1.25 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
## plot3()
##
## Purpose: Look at time-series data, plotting various sub meters for two days in February
##
## Note: Assumes the dataset has already been unzipped and is sitting in the current working directory
plot3 <- function() {
## Read in the data
data <- read.table("household_power_consumption.txt", header = T, sep = ";", colClasses = c(NA, NA, "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"), stringsAsFactors = F, na.strings = "?")
## Convert dates/times from character
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
data$Time <- strptime(data$Time, format = "%H:%M:%S")
## Restrict the dates to the first two days in February 2007
data <- data[(data$Date > "2007-01-31" & data$Date < "2007-02-03"), ]
## Plot the sub metering data as a time series; generate PNG
png(filename = "plot3.png")
plot(data$DateTime, data$Sub_metering_1, main = "", type = "n", xlab = "", ylab = "Energy sub metering")
lines(data$DateTime, data$Sub_metering_1)
lines(data$DateTime, data$Sub_metering_2, col = "red")
lines(data$DateTime, data$Sub_metering_3, col = "blue")
legend("topright", pch = 1, col = c("black", "blue", "red"), legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
dev.off()
}