Exploratory analysis of “Individual household electric power consumption” Data Set from University of California Machine Learning Repository to examine household energy usage over a 2-day period in February 2007.
#Download File
if(!file.exists("exdata-data-household_power_consumption.zip")) {
temp <- tempfile()
download.file("http://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip",temp)
file <- unzip(temp)
unlink(temp)
}
#Read table
power <- read.table(file, header=T, sep=";")
#Format dates
power$Date <- as.Date(power$Date, format="%d/%m/%Y")
#Subset data for 2/1/2007 and 2/2/2007
df <- power[(power$Date=="2007-02-01") | (power$Date=="2007-02-02"),]
df$Global_active_power <- as.numeric(as.character(df$Global_active_power))
df$Global_reactive_power <- as.numeric(as.character(df$Global_reactive_power))
df$Voltage <- as.numeric(as.character(df$Voltage))
df <- transform(df, timestamp=as.POSIXct(paste(Date, Time)), "%d/%m/%Y %H:%M:%S")
df$Sub_metering_1 <- as.numeric(as.character(df$Sub_metering_1))
df$Sub_metering_2 <- as.numeric(as.character(df$Sub_metering_2))
df$Sub_metering_3 <- as.numeric(as.character(df$Sub_metering_3))
plot1 <- function() {
hist(df$Global_active_power, main = paste("Global Active Power"), col="gold", xlab="Global Active Power (kilowatts)")
dev.copy(png, file="ucplot1.png", width=480, height=480)
dev.off()
}
plot1()
png
2
plot2 <- function() {
plot(df$timestamp,df$Global_active_power, type="l", xlab="", ylab="Global Active Power (kilowatts)")
dev.copy(png, file="ucplot2.png", width=480, height=480)
dev.off()
}
plot2()
png
2
plot3 <- function() {
plot(df$timestamp,df$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
lines(df$timestamp,df$Sub_metering_2,col="red")
lines(df$timestamp,df$Sub_metering_3,col="blue")
legend("topright", col=c("black","red","blue"), c("Sub_metering_1 ","Sub_metering_2 ", "Sub_metering_3 "),lty=c(1,1), lwd=c(1,1))
dev.copy(png, file="ucplot3.png", width=480, height=480)
dev.off()
}
plot3()
png
2
plot4 <- function() {
par(mfrow=c(2,2))
#Plot 1
plot(df$timestamp,df$Global_active_power, type="l", xlab="", ylab="Global Active Power")
#Plot 2
plot(df$timestamp,df$Voltage, type="l", xlab="datetime", ylab="Voltage")
#Plot 3
plot(df$timestamp,df$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
lines(df$timestamp,df$Sub_metering_2,col="red")
lines(df$timestamp,df$Sub_metering_3,col="blue")
legend("topright", col=c("black","red","blue"),
c("Sub_metering_1 ","Sub_metering_2 ", "Sub_metering_3 "),lty=c(1,1), bty="n", cex=.5)
#bty removes the box, cex shrinks the text, spacing added after labels so it renders correctly
#Plot 4
plot(df$timestamp,df$Global_reactive_power, type="l", xlab="datetime", ylab="Global_reactive_power")
#Output
dev.copy(png, file="ucplot4.png", width=480, height=480)
dev.off()
}
plot4()
png
2