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
25 lines (20 loc) · 963 Bytes
/
plot3.R
File metadata and controls
25 lines (20 loc) · 963 Bytes
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
# load libraries
library(data.table, quietly=TRUE)
## Read data and subset dates
cons <- fread("household_power_consumption.txt", na.strings="?")[Date=="1/2/2007" | Date=="2/2/2007"]
## change locale to English
Sys.setlocale("LC_TIME", "English")
## Convert Date and Time to classes Date and POSIXlt and add column Week_day
datetime <- strptime(paste(cons$Date, cons$Time),format="%d/%m/%Y %H:%M:%S")
cons$Date <- as.Date(cons$Date, format="%d/%m/%Y")
cons[,Week_day:=weekdays.Date(Date)]
## Plot graphic
png("plot3.png")
plot(datetime, cons$Sub_metering_1, ylim=c(0,38), xlab="", ylab="Energy sub metering", type="n")
lines(datetime, cons$Sub_metering_1, col="black")
lines(datetime, cons$Sub_metering_2, col="red")
lines(datetime, cons$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()
## restore locale to Spanish
Sys.setlocale("LC_TIME", "Spanish")