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
35 lines (28 loc) · 1.18 KB
/
Copy pathplot3.R
File metadata and controls
35 lines (28 loc) · 1.18 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
# read the table - since we only want one day in 2007, speed this up by
# not reading past 200,000 lines. That number was derived by looking at
# the data - we stop somewhere in April, which is plenty
# Read the file, stop after
data = read.table("household_power_consumption.txt",
header = TRUE,
sep = ";",
na.strings = "?",
comment.char = "",
nrows = 200000)
# convert dates from factors to Dates/Times so we can compare them
data$Date <- strptime(paste(data$Date,data$Time), "%d/%m/%Y %H:%M:%S")
# now get the subset of the data we care about
theDay <- subset(data, Date >= "2007-02-01 00:00:00"
& Date <= "2007-02-02 24:00:00")
# draw the plot into the file
png('plot3.png')
plot(theDay$Date, theDay$Sub_metering_1, type="l",
xlab = "", ylab = "Energy sub metering")
# add the second and third lines
lines(theDay$Date, theDay$Sub_metering_2, col="red")
lines(theDay$Date, theDay$Sub_metering_3, col="blue")
# add the legend
legend("topright",
c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
lty=c(1, 1, 1),
col=c("black", "red", "blue"))
dev.off()