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 (21 loc) · 1.25 KB
/
plot3.R
File metadata and controls
25 lines (21 loc) · 1.25 KB
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
Sys.setlocale(locale="English_United States.1252")
library(data.table)
all <- fread('household_power_consumption.txt', na.strings=c("?"), colClasses=c('character', 'character', rep('numeric',7)))
## create a subset for the given 2 days
days <- subset(all, all$Date == '1/2/2007' | all$Date == '2/2/2007')
## add a column consisting of the date and time to create a single datetime entry
days$when <- paste(days$Date, days$Time)
## convert the date time.
days$when <- as.POSIXct(strptime(paste(days$Date, days$Time), format='%d/%m/%Y %H:%M:%S'))
## for some reason the numerics were still converted to characters even with na.strings attributes
## so cast the values to numerics
days$Sub_metering_1 <- as.numeric(days$Sub_metering_1)
days$Sub_metering_2 <- as.numeric(days$Sub_metering_2)
days$Sub_metering_3 <- as.numeric(days$Sub_metering_3)
png("plot3.png", width = 480, height = 480)
plot(days$when, days$Sub_metering_1, type = 'n',xlab='', ylab='Energy sub meeting')
points(days$when, days$Sub_metering_1, type='l')
points(days$when, days$Sub_metering_2, type='l', col='red')
points(days$when, days$Sub_metering_3, type='l', col='blue')
legend('topright', c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), lty=c(1,1), lwd=c(3,3), col=c('black', 'red', 'blue'))
dev.off()