forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
34 lines (27 loc) · 1.61 KB
/
plot4.R
File metadata and controls
34 lines (27 loc) · 1.61 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
26
27
28
29
30
31
32
33
34
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)
days$Voltage <- as.numeric(days$Voltage)
png("plot4.png", width = 480, height = 480)
par(mfrow=c(2,2))
par(mar=c(4,4,1,1))
plot(days$Global_active_power ~ days$when, type='l', xlab='', ylab='Gobal Active Power')
plot(days$when, days$Voltage, xlab = 'datetime', 'ylab' = 'Voltage', type= 'l')
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'),bty='n', lty=c(1,1), lwd=c(3,3), col=c('black', 'red', 'blue'))
plot(days$when, days$Global_reactive_power, type = 'l',xlab='datetime', ylab = 'Global_reactive_power')
dev.off()