ExData_Plotting1

This is the main R markdown file for the Exploratory Data Analysis course project 1. The goal of this project is to examine how household energy usage varies over a 2-day period in February, 2007. The data is taken from the UCI Machine Learning Repository and is available at the following link: https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip

The individual plots are created using functions defined in the plot1.R, plot2.R, plot3.R, and plot4.R files. The data is processed in the main.R file.

Introduction

First, we will download and unzip the data. Then, we will load the data and subset it to the relevant time period. Finally, we will create the plots.

Data Processing

# Download and unzip the data
data_source <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zip_file <- "./data/household_power_consumption.zip"
file_path <- "./data/household_power_consumption.txt"

# If data directory does not exist, create it
if (!file.exists("./data")) {
    dir.create("./data")
}

# If the file does not exist, download it and unzip it
if (!file.exists(file_path)) {
    download.file(data_source, zip_file, method = "curl")
    unzip(zip_file, exdir = "./data")
}

# Load the data
data <- read.table("./data/household_power_consumption.txt", header = TRUE, sep = ";", na.strings = "?")

Let’s take a look at the data

class(data)
## [1] "data.frame"
head(data)
##         Date     Time Global_active_power Global_reactive_power Voltage
## 1 16/12/2006 17:24:00               4.216                 0.418  234.84
## 2 16/12/2006 17:25:00               5.360                 0.436  233.63
## 3 16/12/2006 17:26:00               5.374                 0.498  233.29
## 4 16/12/2006 17:27:00               5.388                 0.502  233.74
## 5 16/12/2006 17:28:00               3.666                 0.528  235.68
## 6 16/12/2006 17:29:00               3.520                 0.522  235.02
##   Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
## 1             18.4              0              1             17
## 2             23.0              0              1             16
## 3             23.0              0              2             17
## 4             23.0              0              1             17
## 5             15.8              0              1             17
## 6             15.0              0              2             17

We are only interested in the data from 2007-02-01 to 2007-02-02. Let’s subset the data

# Convert Date and Time variables to Date/Time classes in R using the strptime() and as.Date() functions.
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
data$Time <- strptime(data$Time, format = "%H:%M:%S")

# Subset the data
data_sub <- subset(data, Date %in% as.Date(c("2007-02-01", "2007-02-02")))

# Combine the Date and Time variables to create a single Date/Time variable
data_sub$DateTime <- as.POSIXct(paste(data_sub$Date, format(data_sub$Time, "%H:%M:%S")), format = "%Y-%m-%d %H:%M:%S")

Let’s take a look at the subsetted data

print(class(data_sub))
## [1] "data.frame"
head(data_sub)
##             Date                Time Global_active_power Global_reactive_power
## 66637 2007-02-01 2024-03-09 00:00:00               0.326                 0.128
## 66638 2007-02-01 2024-03-09 00:01:00               0.326                 0.130
## 66639 2007-02-01 2024-03-09 00:02:00               0.324                 0.132
## 66640 2007-02-01 2024-03-09 00:03:00               0.324                 0.134
## 66641 2007-02-01 2024-03-09 00:04:00               0.322                 0.130
## 66642 2007-02-01 2024-03-09 00:05:00               0.320                 0.126
##       Voltage Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
## 66637  243.15              1.4              0              0              0
## 66638  243.32              1.4              0              0              0
## 66639  243.51              1.4              0              0              0
## 66640  243.90              1.4              0              0              0
## 66641  243.16              1.4              0              0              0
## 66642  242.29              1.4              0              0              0
##                  DateTime
## 66637 2007-02-01 00:00:00
## 66638 2007-02-01 00:01:00
## 66639 2007-02-01 00:02:00
## 66640 2007-02-01 00:03:00
## 66641 2007-02-01 00:04:00
## 66642 2007-02-01 00:05:00

Plots

Plot 1

Our goal is to examine how household energy usage varies over a 2-day period in February, 2007.

# Plot 1
source("plot1.R")
plot1(data_sub)

# Save the plot
dev.copy(png, "plot1.png", width = 480, height = 480)
## quartz_off_screen 
##                 3

Plot 2

Our goal is to examine how Global Active Power varies over a 2-day period in February, 2007 on Thursday, Friday, and Saturday.

# Plot 2
# Plot a line graph of Global Active Power over the 2-day period
# Combine the Date and Time variables to create a single Date/Time variable
source("plot2.R")
plot2(data_sub)

dev.copy(png, "plot2.png", width = 480, height = 480)
## quartz_off_screen 
##                 4

Plot 3

Our goal is show Energy Sub Metering over the 2-day period in February, 2007 factored by sub metering type.

# Plot 3
source("plot3.R")
plot3(data_sub)

dev.copy(png, "plot3.png", width = 480, height = 480)
## quartz_off_screen 
##                 5

Plot 4

The goal is to plot a 2 x 2 grid of plots showing the following: - Plot 2 in the upper left corner - A new plot containing Voltage vs DateTime in the upper right corner - Plot 3 in the lower left corner - A new plot containing Global Reactive Power vs DateTime in the lower right corner

# Plot 4
source("plot4.R")
plot4(data_sub)

dev.copy(png, "plot4.png", width = 480, height = 480)
## quartz_off_screen 
##                 6