diff --git a/plot1.R b/plot1.R new file mode 100644 index 0000000000..defb4b996b --- /dev/null +++ b/plot1.R @@ -0,0 +1,19 @@ +library(data.table) + +## 1. Load data +download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", "./electric_power_data.zip") +unzip("electric_power_data.zip","household_power_consumption.txt") +data <- fread("household_power_consumption.txt", stringsAsFactors = F, sep = ";") + +## 2. Pre-process +df <- data[Date %in% c('1/2/2007', '2/2/2007'),] +df$Date <- as.Date(df$Date, "%d/%m/%Y") +df$Time <- strptime(df$Time, "%H:%M:%S") +df$Global_active_power <- as.numeric(df$Global_active_power) + +## 3. Plot 1 +png("plot1.png") +hist(df$Global_active_power, main = "Global Active Power", xlab = "Global Active Power (kilowatts)", col = "red") + +## 4. Close connection +dev.off() diff --git a/plot1.png b/plot1.png new file mode 100644 index 0000000000..42d3c3dbd2 Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 0000000000..2c3ef84681 --- /dev/null +++ b/plot2.R @@ -0,0 +1,20 @@ +library(data.table) + +## 1. Load data +download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", "./electric_power_data.zip") +unzip("electric_power_data.zip","household_power_consumption.txt") +data <- fread("household_power_consumption.txt", stringsAsFactors = F, sep = ";") + +## 2. Pre-process +df <- data[Date %in% c('1/2/2007', '2/2/2007'),] +df$Date <- as.Date(df$Date, "%d/%m/%Y") +df$Time <- strptime(df$Time, "%H:%M:%S") +df$Global_active_power <- as.numeric(df$Global_active_power) +df$Datetime <- as.POSIXct(paste(df$Date, df$Time), format="%Y-%m-%d %H:%M:%S") + +## 3. Plot 2 +png("plot2.png") +plot(df$Global_active_power ~ df$Datetime, type = "l", main = "", ylab = "Global Active Power (kilowatts)", xlab = "") + +## 4. Close connection +dev.off() diff --git a/plot2.png b/plot2.png new file mode 100644 index 0000000000..87d17c13e4 Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 0000000000..f1208aa26d --- /dev/null +++ b/plot3.R @@ -0,0 +1,27 @@ +library(data.table) + +## 1. Load data +download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", "./electric_power_data.zip") +unzip("electric_power_data.zip","household_power_consumption.txt") +data <- fread("household_power_consumption.txt", stringsAsFactors = F, sep = ";") + +## 2. Pre-process +df <- data[Date %in% c('1/2/2007', '2/2/2007'),] +df$Date <- as.Date(df$Date, "%d/%m/%Y") +df$Time <- strptime(df$Time, "%H:%M:%S") +df$Datetime <- as.POSIXct(paste(df$Date, df$Time), format="%Y-%m-%d %H:%M:%S") +df$Sub_metering_1 <- as.numeric(df$Sub_metering_1) +df$Sub_metering_2 <- as.numeric(df$Sub_metering_2) +df$Sub_metering_3 <- as.numeric(df$Sub_metering_3) + + +## 3. Plot 3 +png("plot3.png") +plot(df$Sub_metering_1 ~ df$Datetime, type = "l", main = "", ylab = "Energy sub metering", xlab = "") +lines(df$Sub_metering_2 ~ df$Datetime, col = "red") +lines(df$Sub_metering_3 ~ df$Datetime, col = "blue") +legend("topright", legend = c("Sub_metering_1", "Sub_metering_1", "Sub_metering_1"), + lwd = 3, col = c("black", "red", "blue")) + +## 4. Close connection +dev.off() diff --git a/plot3.png b/plot3.png new file mode 100644 index 0000000000..79a78ff969 Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 0000000000..3528854f5f --- /dev/null +++ b/plot4.R @@ -0,0 +1,44 @@ +library(data.table) + +## 1. Load data +download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", "./electric_power_data.zip") +unzip("electric_power_data.zip","household_power_consumption.txt") +data <- fread("household_power_consumption.txt", stringsAsFactors = F, sep = ";") + + +## 2. Pre-process +df <- data[Date %in% c('1/2/2007', '2/2/2007'),] +df$Date <- as.Date(df$Date, "%d/%m/%Y") +df$Time <- strptime(df$Time, "%H:%M:%S") +df$Datetime <- as.POSIXct(paste(df$Date, df$Time), format="%Y-%m-%d %H:%M:%S") +df$Global_active_power <- as.numeric(df$Global_active_power) +df$Sub_metering_1 <- as.numeric(df$Sub_metering_1) +df$Sub_metering_2 <- as.numeric(df$Sub_metering_2) +df$Sub_metering_3 <- as.numeric(df$Sub_metering_3) +df$Voltage <- as.numeric(df$Voltage) +df$Global_reactive_power <- as.numeric(df$Global_reactive_power) + + +## 3. Plot 4 +png("plot4.png") +par(mfrow=c(2,2)) + +# 1 +plot(df$Global_active_power ~ df$Datetime, type = "l", main = "", ylab = "Global Active Power", xlab = "") + +# 2 +plot(df$Voltage ~ df$Datetime, type = "l", main = "", ylab = "Voltage", xlab = "datetime") + +# 3 +plot(df$Sub_metering_1 ~ df$Datetime, type = "l", main = "", ylab = "Energy sub metering", xlab = "") +lines(df$Sub_metering_2 ~ df$Datetime, col = "red") +lines(df$Sub_metering_3 ~ df$Datetime, col = "blue") +legend("topright", legend = c("Sub_metering_1", "Sub_metering_1", "Sub_metering_1"), + lwd = 3, col = c("black", "red", "blue"), bty = "n") + +# 4 +plot(df$Global_reactive_power ~ df$Datetime, type = "l", main = "", ylab = "Global_reactive_power", xlab = "datetime") + + +## 4. Close connection +dev.off() diff --git a/plot4.png b/plot4.png new file mode 100644 index 0000000000..d58a50337e Binary files /dev/null and b/plot4.png differ