forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
49 lines (41 loc) · 1.38 KB
/
plot3.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
42
43
44
45
46
47
48
49
#
# plot the 3rd figure for the class project 1
#
# load necessary libraries
library(data.table)
# flag to enable debug
debug <- FALSE
####################################
# load the data
#
# get the column names
colnames <- fread("household_power_consumption.txt", sep=";", nrows=0)
# read in only the rows corresponding to 2007-02-01 and 2007-02-02
data <- fread("household_power_consumption.txt", sep=";", na.string="?", skip=66637, nrows=2880)
# update the column names of data
setnames(data, names(data), names(colnames))
if(!debug) {
####################################
# Prepare the graph device
#
png(file="plot3.png", width=480, height=480)
}
####################################
# Plot the values of Global Active power
#
# extract the time information from the Date and Time columns
time <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
# plot the first metering, black line
plot(time, data$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
# plot the 2nd metering, red line
lines(time, data$Sub_metering_2, type="l", col="red")
# plot the 3rd metering, blue line
lines(time, data$Sub_metering_3, type="l", col="blue")
# plot the legend
legend("topright", lty=1, col=c("black", "blue", "red"), legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
if(!debug) {
####################################
# Close the graph device
#
dev.off()
}