forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
24 lines (21 loc) · 970 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
# read in the entire data
dt <- read.table("household_power_consumption.txt",
header=TRUE, sep=";", dec=".",
stringsAsFactors=FALSE, na.strings= "?",
colClasses=c(rep("character", 2), rep("numeric", 7)))
library(lubridate)
# convert date, time and subset the right data
dt <- dt[dt$Date == "1/2/2007" | dt$Date == "2/2/2007", ]
dt$DateTime = paste(dt$Date, dt$Time, sep=" ")
dt$DateTime <- dmy_hms(dt$DateTime)
# plot to png file
png(file = "plot3.png")
with(dt, plot(DateTime, Sub_metering_1, type="l", col="Black",
xlab="", ylab="Energy Sub Metering"))
# lines add plot to the created one
with(dt, lines(DateTime, Sub_metering_2, type="l",col="Red"))
with(dt, lines(DateTime, Sub_metering_3, type="l", col="Blue"))
# lwd=1 and lty=1 for the line legend
legend("topright", col=c("Black", "Blue", "Red"), lwd=1, lty=1,
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
dev.off()