forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
23 lines (20 loc) · 1.13 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
library(downloader)
library(lubridate)
zip_file <- "exdata%2Fdata%2Fhousehold_power_consumption.zip"
data_file <- "household_power_consumption.txt"
if (!file.exists(data_file)){
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download(url, dest=zip_file, mode="wb")
unzip (zip_file)
file.remove(zip_file)
}
epc <- read.table(data_file, header=TRUE, sep=";", na.strings = "?")
epc$Datetime <- dmy_hms(paste(epc$Date, epc$Time))
epc_sub <- subset(epc, (Datetime >= ymd_hms("2007-02-01 0:0:0") & Datetime <= ymd_hms("2007-02-02 23:59:59")))
png(file="plot3.png", width=480, height = 480)
with(epc_sub, plot(Datetime, Global_active_power, type = "l", xlab="", ylab="Global Active Power (kilowatts)"))
with(epc_sub, plot(Datetime, Sub_metering_1, type = "l", xlab="", ylab="Energy sub metering", col="black"))
with(epc_sub, points(Datetime, Sub_metering_2, type = "l", col="red"))
with(epc_sub, points(Datetime, Sub_metering_3, type = "l", col="blue"))
legend("topright", col = c("black", "red", "blue"), legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lwd = 1)
dev.off()