forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
34 lines (25 loc) · 885 Bytes
/
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
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running
png(filename = "plot3.png")
lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]
t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)
t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")
plot(
t$DateTime,
t$Sub_metering_1,
type="n",
ylab="Energy sub metering",
xlab="")
lines(t$DateTime, t$Sub_metering_1, col="black")
lines(t$DateTime, t$Sub_metering_2, col="red")
lines(t$DateTime, t$Sub_metering_3, col="blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lwd = 1)
dev.off()