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
22 lines (19 loc) · 976 Bytes
/
Copy pathplot3.R
File metadata and controls
22 lines (19 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
plot3 <- function(x){
## Read the file and select relevant rows
hpc <- read.delim("household_power_consumption.txt", sep=";", na.strings = "?")
hpc_1 <- filter(hpc, Date == "1/2/2007")
hpc_2 <- filter(hpc, Date == "2/2/2007")
hpc <- rbind(hpc_1, hpc_2)
## Create datetime column as class "Date"
hpc$Date <- as.Date(hpc$Date, "%d/%m/%Y")
datetime <- paste(as.character(hpc$Date), as.character(hpc$Time))
datetime <- strptime(datetime, c("%Y-%m-%d%T"))
hpc <- cbind(hpc, datetime)
## Save plot to png file
png(file="plot3.png")
with(hpc, plot(datetime, Sub_metering_1, type="l", ylab = "Energy sub metering", xlab =""),legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
with(hpc, lines(datetime, Sub_metering_2, col = "red"))
with(hpc, lines(datetime, Sub_metering_3, col = "blue"))
legend("topright", lty = 1, col = c("black","red","blue"), legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
dev.off()
}