Objective

To explore how household power usage varies on dates 2007-02-01 and 2007-02-02.

Procedures

1. Load data

rawdata <- read.table("household_power_consumption.txt", header = TRUE, sep = ";", stringsAsFactors = FALSE, na.strings = "?")
# colClasses = c("Date", "Time", rep("num",7))

sub <- rawdata[rawdata$Date %in% c("1/2/2007","2/2/2007"),]

2. Basic checking

str(sub)
## 'data.frame':    2880 obs. of  9 variables:
##  $ Date                 : chr  "1/2/2007" "1/2/2007" "1/2/2007" "1/2/2007" ...
##  $ Time                 : chr  "00:00:00" "00:01:00" "00:02:00" "00:03:00" ...
##  $ Global_active_power  : num  0.326 0.326 0.324 0.324 0.322 0.32 0.32 0.32 0.32 0.236 ...
##  $ Global_reactive_power: num  0.128 0.13 0.132 0.134 0.13 0.126 0.126 0.126 0.128 0 ...
##  $ Voltage              : num  243 243 244 244 243 ...
##  $ Global_intensity     : num  1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1.4 1 ...
##  $ Sub_metering_1       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Sub_metering_2       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Sub_metering_3       : num  0 0 0 0 0 0 0 0 0 0 ...
head(sub)
##           Date     Time Global_active_power Global_reactive_power Voltage
## 66637 1/2/2007 00:00:00               0.326                 0.128  243.15
## 66638 1/2/2007 00:01:00               0.326                 0.130  243.32
## 66639 1/2/2007 00:02:00               0.324                 0.132  243.51
## 66640 1/2/2007 00:03:00               0.324                 0.134  243.90
## 66641 1/2/2007 00:04:00               0.322                 0.130  243.16
## 66642 1/2/2007 00:05:00               0.320                 0.126  242.29
##       Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
## 66637              1.4              0              0              0
## 66638              1.4              0              0              0
## 66639              1.4              0              0              0
## 66640              1.4              0              0              0
## 66641              1.4              0              0              0
## 66642              1.4              0              0              0
tail(sub)
##           Date     Time Global_active_power Global_reactive_power Voltage
## 69511 2/2/2007 23:54:00               3.696                 0.226  240.71
## 69512 2/2/2007 23:55:00               3.696                 0.226  240.90
## 69513 2/2/2007 23:56:00               3.698                 0.226  241.02
## 69514 2/2/2007 23:57:00               3.684                 0.224  240.48
## 69515 2/2/2007 23:58:00               3.658                 0.220  239.61
## 69516 2/2/2007 23:59:00               3.680                 0.224  240.37
##       Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
## 69511             15.2              0              1             17
## 69512             15.2              0              1             18
## 69513             15.2              0              2             18
## 69514             15.2              0              1             18
## 69515             15.2              0              1             17
## 69516             15.2              0              2             18

3. Visualising

hist(sub$Global_active_power, col = "red", xlab = "Global Active Power (kilowatts)", main = "Global Active Power")

dev.copy(png, file = "plot1.png", height = 480, width = 480)
## quartz_off_screen 
##                 3
dev.off()
## quartz_off_screen 
##                 2
datetime <- strptime(paste0(sub$Date, sub$Time), "%d/%m/%Y %H:%M:%S")#
plot(datetime ,sub$Global_active_power, type = "l", ylab = "Global Active Power (kilowatts)", xlab = "")

dev.copy(png, file = "plot2.png", height = 480, width = 480)
## quartz_off_screen 
##                 3
dev.off()
## quartz_off_screen 
##                 2
plot(datetime, sub$Sub_metering_1, xlab = "", ylab = "Energy sub meltering", type = "l")
lines(datetime, sub$Sub_metering_2, col = "red")
lines(datetime, sub$Sub_metering_3, col = "blue")
legend("topright", c("Sub_meltering_1", "Sub_meltering_2","Sub_meltering_3"), col = c("black","red","blue"), lty = 1, lwd = 2)

dev.copy(png, file = "plot3.png", height = 480, width = 480)
## quartz_off_screen 
##                 3
dev.off()
## quartz_off_screen 
##                 2
par(mfrow = c(2,2))

plot(datetime ,sub$Global_active_power, type = "l", ylab = "Global Active Power (kilowatts)", xlab = "")

plot(datetime, sub$Voltage, type = "l", ylab = "Voltage")

plot(datetime, sub$Sub_metering_1, xlab = "", ylab = "Energy sub meltering", type = "l")
lines(datetime, sub$Sub_metering_2, col = "red")
lines(datetime, sub$Sub_metering_3, col = "blue")
legend("topright", c("Sub_meltering_1", "Sub_meltering_2","Sub_meltering_3"), col = c("black","red","blue"), lty = 1, lwd = 2)

plot(datetime, sub$Global_reactive_power, type = "l")

dev.copy(png, file = "plot4.png", height = 480, width = 480)
## quartz_off_screen 
##                 3
dev.off()
## quartz_off_screen 
##                 2