diff --git a/Plot 1.R b/Plot 1.R new file mode 100644 index 00000000000..b0eb05d040c --- /dev/null +++ b/Plot 1.R @@ -0,0 +1,20 @@ +library(lubridate) + +if(!file.exists("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")){dir.create("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")} +fileurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(fileurl, destfile="household_power_consumption.zip") + +unzip(zipfile = "household_power_consumption.zip", exdir = "C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption") + +##read data into R and Subset based on the Date needed +importdata <- read.csv("~/MyProject/Learning R/ExData_Plotting1/household_power_consumption/household_power_consumption.txt", sep=";") + +power_consumption_feb <- subset(importdata, Date == "1/2/2007" | Date == "2/2/2007") + +power_consumption_feb$Date <- as.Date(parse_date_time(power_consumption_feb$Date, c('dmy'))) +power_consumption_feb$Global_active_power <- as.numeric(power_consumption_feb$Global_active_power) + +png(filename = "Plot 1.png", width = 480, height = 480) +with(power_consumption_feb, hist(Global_active_power, col = "red", xlab = "Global Active Power (kilowatts)", main = paste("Global Active Power"))) +dev.off() + diff --git a/Plot 1.png b/Plot 1.png new file mode 100644 index 00000000000..4834ed86a72 Binary files /dev/null and b/Plot 1.png differ diff --git a/Plot 2.R b/Plot 2.R new file mode 100644 index 00000000000..a72991b2401 --- /dev/null +++ b/Plot 2.R @@ -0,0 +1,18 @@ +library(lubridate) +library(dplyr) + +if(!file.exists("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")){dir.create("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")} +fileurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(fileurl, destfile="household_power_consumption.zip") + +unzip(zipfile = "household_power_consumption.zip", exdir = "C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption") + +##read data into R and Subset based on the Date needed +importdata <- read.csv("~/MyProject/Learning R/ExData_Plotting1/household_power_consumption/household_power_consumption.txt", sep=";") + +power_consumption_feb <- subset(importdata, Date == "1/2/2007" | Date == "2/2/2007") %>% mutate(Date = as.POSIXct(dmy_hms(as.character(paste(Date, Time)))), + Global_active_power = as.numeric(as.character(Global_active_power))) %>% select(Date,Global_active_power) + +png(filename = "Plot 2.png", width = 480, height = 480) +with(power_consumption_feb, plot (Date, Global_active_power, type="l", xlab = "", ylab = "Global Active Power (kilowatts)")) +dev.off() diff --git a/Plot 2.png b/Plot 2.png new file mode 100644 index 00000000000..d60b38b0d0f Binary files /dev/null and b/Plot 2.png differ diff --git a/Plot 3.R b/Plot 3.R new file mode 100644 index 00000000000..0f30693a3f8 --- /dev/null +++ b/Plot 3.R @@ -0,0 +1,24 @@ +library(lubridate) +library(dplyr) + +if(!file.exists("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")){dir.create("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")} +fileurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(fileurl, destfile="household_power_consumption.zip") + +unzip(zipfile = "household_power_consumption.zip", exdir = "C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption") + +##read data into R and Subset based on the Date needed +importdata <- read.csv("~/MyProject/Learning R/ExData_Plotting1/household_power_consumption/household_power_consumption.txt", sep=";") + +power_consumption_feb <- subset(importdata, Date == "1/2/2007" | Date == "2/2/2007") %>% mutate(Date = as.POSIXct(dmy_hms(as.character(paste(Date, Time)))), + Global_active_power = as.numeric(as.character(Global_active_power)), sub_metering_1 = as.numeric(as.character(Sub_metering_1)), + sub_metering_2 = as.numeric(as.character(Sub_metering_2))) %>% select(Date,sub_metering_1,sub_metering_2,Sub_metering_3) + +png(filename = "Plot 3.png", width = 480, height = 480) +with(power_consumption_feb, plot(Date,sub_metering_1, type="n", xlab = "", ylab = "Energy Sub Metering")) +with(power_consumption_feb, points(Date,sub_metering_1, col="black", type="l")) +with(power_consumption_feb, points(Date,sub_metering_2, col="red", type="l")) +with(power_consumption_feb, points(Date,Sub_metering_3, col="blue", type="l")) +legend("topright", lty=1, col = c("black", "red", "blue"), + legend = c("Sub_Metering_1", "Sub_Metering_2", "Sub_Metering_3")) +dev.off() \ No newline at end of file diff --git a/Plot 3.png b/Plot 3.png new file mode 100644 index 00000000000..85619ce7b89 Binary files /dev/null and b/Plot 3.png differ diff --git a/Plot 4.R b/Plot 4.R new file mode 100644 index 00000000000..b532f0e0fd2 --- /dev/null +++ b/Plot 4.R @@ -0,0 +1,33 @@ +library(lubridate) +library(dplyr) + +if(!file.exists("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")){dir.create("C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption")} +fileurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(fileurl, destfile="household_power_consumption.zip") + +unzip(zipfile = "household_power_consumption.zip", exdir = "C:\\Users\\sdanilola\\Documents\\MyProject\\Learning R\\ExData_Plotting1\\household_power_consumption") + +##read data into R and Subset based on the Date needed +importdata <- read.csv("~/MyProject/Learning R/ExData_Plotting1/household_power_consumption/household_power_consumption.txt", sep=";") + +power_consumption <- subset(importdata, Date == "1/2/2007" | Date == "2/2/2007") %>% mutate(Date = as.POSIXct(dmy_hms(as.character(paste(Date, Time)))), + global_active_power= as.numeric(as.character(Global_active_power)), + global_reactive_power = as.numeric(as.character(Global_reactive_power)), + voltage = as.numeric(as.character(Voltage)), + sub_metering_1 = as.numeric(as.character(Sub_metering_1)), + sub_metering_2 = as.numeric(as.character(Sub_metering_2)), + sub_metering_3 = as.numeric(as.character(Sub_metering_3))) +png(filename = "Plot 4.png", width = 480, height = 480) + +par(mfrow = c(2, 2)) +with(power_consumption, plot(Date,global_active_power, col = "black", type = "l", xlab = "", ylab = "Global Active Power (kilowatts)")) +with(power_consumption, plot(Date,voltage, col = "black", type = "l", xlab = "datetime", ylab = "Voltage")) +with(power_consumption, plot(Date,sub_metering_1, type="n", xlab = "", ylab = "Energy Sub Metering")) +with(power_consumption, points(Date,sub_metering_1, col="black", type="l")) +with(power_consumption, points(Date,sub_metering_2, col="red", type="l")) +with(power_consumption, points(Date,sub_metering_3, col="blue", type="l")) +legend("topright", lty=1, col = c("black", "red", "blue"), + legend = c("Sub_Metering_1", "Sub_Metering_2", "Sub_Metering_3")) + +with(power_consumption, plot(Date,global_reactive_power, col = "black", type = "l", xlab = "Global Reactive Power", ylab = "datetime")) +dev.off() diff --git a/Plot 4.png b/Plot 4.png new file mode 100644 index 00000000000..fee0c99af25 Binary files /dev/null and b/Plot 4.png differ