forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
31 lines (24 loc) · 1.04 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
24
25
26
27
28
29
30
31
## Getting dataset with fread from data.table package
data <- fread("./Data/household_power_consumption.txt", na.strings = "?")
## Converting classes
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
data$Global_active_power <- as.numeric(data$Global_active_power)
data$Sub_metering_1 <- as.numeric(data$Sub_metering_1)
data$Sub_metering_2 <- as.numeric(data$Sub_metering_2)
data$Sub_metering_3 <- as.numeric(data$Sub_metering_3)
## Subsetting the data
data <- data[Date == "2007/02/01" | Date == "2007/02/02" ]
## Adding and converting new variables
data[,DateTime:=as.POSIXct(paste(data$Date, data$Time))]
## Open graphics device
png(filename="plot3.png",width=480,height=480)
## Plot
attach(data)
plot(Sub_metering_1~DateTime, type = "l", ylab = "Energy sub metering", xlab = "")
lines(Sub_metering_2~DateTime, col = "red")
lines(Sub_metering_3~DateTime, col = "blue")
## Adding legend
legend("topright", col=c("black", "red", "blue"), lty=1, lwd=2,
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
## Close graphics device
dev.off()