diff --git a/plot1.R b/plot1.R new file mode 100644 index 00000000000..24d4234a268 --- /dev/null +++ b/plot1.R @@ -0,0 +1,43 @@ +##Make sure to download and unzip the file (Source = https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip) +##set your working directory and check that "household_power_consumption.txt" is located inside +##list.files() = "household_power_consumption.txt" + + +##Read in the "household_power_consumption.txt" +Full_Data_Frame <- read.table("household_power_consumption.txt", + header = T, + sep = ';', + na.strings = "?", + nrows = 2075259, + check.names = F, + stringsAsFactors = F, + comment.char = "", + quote = '\"') + + +##Subsett just the two days we need ("1/2/2007" & "2/2/2007") +Data <- subset(Full_Data_Frame, + Date %in% c("1/2/2007", + "2/2/2007")) + + +##Convert Date & Time +Data$Date <- as.Date(Data$Date, + format = "%d/%m/%Y") + +Data$Time <- format(Data$Time, + format = "%H:%M:%S") + + +##Plot & save the histogram of "Global Active Power" as a png +png("plot1.png", + width = 480, + height = 480) + +hist(Data$Global_active_power, + main = "Global Active Power", + xlab = "Global Active Power (kilowatts)", + ylab = "Frequency", + col = "Red") + +dev.off() \ No newline at end of file diff --git a/plot1.png b/plot1.png new file mode 100644 index 00000000000..db485ff3905 Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 00000000000..d90e0d6473a --- /dev/null +++ b/plot2.R @@ -0,0 +1,53 @@ +##Make sure to download and unzip the file (Source = https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip) +##set your working directory and check that "household_power_consumption.txt" is located inside +##list.files() = "household_power_consumption.txt" + + +##Read in the "household_power_consumption.txt" +Full_Data_Frame <- read.table("household_power_consumption.txt", + header = T, + sep = ';', + na.strings = "?", + nrows = 2075259, + check.names = F, + stringsAsFactors = F, + comment.char = "", + quote = '\"') + + +##Subsett just the two days we need ("1/2/2007" & "2/2/2007") +Data <- subset(Full_Data_Frame, + Date %in% c("1/2/2007", + "2/2/2007")) + + +##Convert Date & Time +Data$Date <- as.Date(Data$Date, + format = "%d/%m/%Y") + +Data$Time <- format(Data$Time, + format = "%H:%M:%S") + + +##Combine $Date & $Time +Date_Time <- paste(as.Date(Data$Date), + Data$Time) + + +##Convert "Date_Time" to POSIXct and add it to "Data" as a new column +Data$Date_Time <- as.POSIXct(Date_Time) + + +##Plot "Date_Time" & "Global_active_power" +png("plot2.png", + width = 480, + height = 480) + +with(Data, + { plot(Global_active_power~Date_Time, + type = "l", + ylab = "Global Active Power (kilowatts)", + xlab = "") } + ) + +dev.off() \ No newline at end of file diff --git a/plot2.png b/plot2.png new file mode 100644 index 00000000000..08381f8befb Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 00000000000..0dd4bc4d23d --- /dev/null +++ b/plot3.R @@ -0,0 +1,71 @@ +##Make sure to download and unzip the file (Source = https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip) +##set your working directory and check that "household_power_consumption.txt" is located inside +##list.files() = "household_power_consumption.txt" + + +##Read in the "household_power_consumption.txt" +Full_Data_Frame <- read.table("household_power_consumption.txt", + header = T, + sep = ';', + na.strings = "?", + nrows = 2075259, + check.names = F, + stringsAsFactors = F, + comment.char = "", + quote = '\"') + + +##Subsett just the two days we need ("1/2/2007" & "2/2/2007") +Data <- subset(Full_Data_Frame, + Date %in% c("1/2/2007", + "2/2/2007")) + + +##Convert Date & Time +Data$Date <- as.Date(Data$Date, + format = "%d/%m/%Y") + +Data$Time <- format(Data$Time, + format = "%H:%M:%S") + + +##Combine $Date & $Time +Date_Time <- paste(as.Date(Data$Date), + Data$Time) + + +##Convert "Date_Time" to POSIXct and add it to "Data" as a new column +Data$Date_Time <- as.POSIXct(Date_Time) + + +##Plot "Date_Time" & "Global_active_power" +png("plot3.png", + width = 480, + height = 480) + +with(Data, + { plot(Sub_metering_1~Date_Time, + type = "l", + ylab = "Energy sub metering", + xlab = "") + + lines(Sub_metering_2~Date_Time, + col = 'Red') + + lines(Sub_metering_3~Date_Time, + col = 'Blue') } + ) + +##Add the legend +legend("topright", + col=c("black", + "red", + "blue"), + lty=1, + lwd=2, + legend = c("Sub_metering_1", + "Sub_metering_2", + "Sub_metering_3")) + + +dev.off() \ No newline at end of file diff --git a/plot3.png b/plot3.png new file mode 100644 index 00000000000..9f289aa8216 Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 00000000000..99e06467299 --- /dev/null +++ b/plot4.R @@ -0,0 +1,93 @@ +##Make sure to download and unzip the file (Source = https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip) +##set your working directory and check that "household_power_consumption.txt" is located inside +##list.files() = "household_power_consumption.txt" + + +##Read in the "household_power_consumption.txt" +Full_Data_Frame <- read.table("household_power_consumption.txt", + header=T, sep=';', + na.strings="?", + nrows=2075259, + check.names=F, + stringsAsFactors=F, + comment.char="", + quote='\"') + + +##Subsett just the two days we need ("1/2/2007" & "2/2/2007") +Data <- subset(Full_Data_Frame, + Date %in% c("1/2/2007", + "2/2/2007")) + + +##Convert Date & Time +Data$Date <- as.Date(Data$Date, + format = "%d/%m/%Y") + +Data$Time <- format(Data$Time, + format = "%H:%M:%S") + + +##Combine $Date & $Time +Date_Time <- paste(as.Date(Data$Date), + Data$Time) + + +##Convert "Date_Time" to POSIXct and add it to "Data" as a new column +Data$Date_Time <- as.POSIXct(Date_Time) + + +##Save +png("plot4.png", + width = 480, + height = 480) + + +##Set the parameters for the graphs +par(mfrow = c(2,2), + mar = c(4,4,2,1), + oma = c(0,0,2,0)) + + +##Plotting (Using 1 function) +with(Data, + { plot(Global_active_power~Date_Time, + type = "l", + ylab = "Global Active Power", + xlab = "") + + plot(Voltage~Date_Time, + type = "l", + ylab = "Voltage", + xlab = "") + + plot(Sub_metering_1~Date_Time, + type = "l", + ylab = "Energy sub metering", + xlab = "") + + lines(Sub_metering_2~Date_Time, + col = 'Red') + + lines(Sub_metering_3~Date_Time, + col = 'Blue') + + legend("topright", + col = c("black", + "red", + "blue"), + lty=1, + lwd=2, + bty="n", + legend = c("Sub_metering_1", + "Sub_metering_2", + "Sub_metering_3")) + + plot(Global_reactive_power~Date_Time, + type = "l", + ylab = "Global_reactive_power", + xlab = "") } + ) + + +dev.off() \ No newline at end of file diff --git a/plot4.png b/plot4.png new file mode 100644 index 00000000000..abdb72f4978 Binary files /dev/null and b/plot4.png differ