forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
``` r | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
The dataset | ||
----------- | ||
|
||
The *Individual household electric power consumption Data Set* is collected from the UC Irvine Machine Learning Repository and made available through the course web site (<https://github.com/rdpeng/ExData_Plotting1>) | ||
|
||
------------------------------------------------------------------------ | ||
|
||
Creating a directory for the dataset | ||
------------------------------------ | ||
|
||
``` r | ||
# make the directory named "data" if it does not exist | ||
if(!file.exists("data")){ | ||
dir.create("data") | ||
} | ||
``` | ||
|
||
------------------------------------------------------------------------ | ||
|
||
------------------------------------------------------------------------ | ||
|
||
Dataset download | ||
---------------- | ||
|
||
``` r | ||
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" | ||
download.file(fileUrl, | ||
destfile="./data/household_power_consumption.zip", | ||
method="curl") | ||
list.files("./data") | ||
``` | ||
|
||
## [1] "household_power_consumption.zip" | ||
|
||
``` r | ||
unzip ("./data/household_power_consumption.zip", | ||
exdir = "./data") | ||
list.files("./data") | ||
``` | ||
|
||
## [1] "household_power_consumption.txt" "household_power_consumption.zip" | ||
|
||
``` r | ||
#dataset download details | ||
dateDownloaded <- date() | ||
dateDownloaded | ||
``` | ||
|
||
## [1] "Sun Jun 23 18:39:49 2019" | ||
|
||
------------------------------------------------------------------------ |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
power.data.subset <- read.table( | ||
"./data/household_power_consumption.txt", | ||
header=T, | ||
na.strings = "?", | ||
stringsAsFactors=F, | ||
sep=";", | ||
nrow=100) | ||
household_power_consumption_all <- read.table( | ||
"./data/household_power_consumption.txt", | ||
na.strings = "?", | ||
header=T, | ||
stringsAsFactors=F, | ||
sep=";", | ||
colClasses = sapply(power.data.subset, class)) | ||
|
||
household_power_consumption_all$Date.Formatted<- as.Date( | ||
household_power_consumption_all$Date, | ||
format="%d/%m/%Y") | ||
|
||
datetime <- paste( | ||
household_power_consumption_all$Date.Formatted, | ||
household_power_consumption_all$Time) | ||
|
||
household_power_consumption_all$datetime <- strptime( | ||
datetime, | ||
"%Y-%m-%d %H:%M:%S") | ||
|
||
dat.for.2days <- subset( | ||
household_power_consumption_all, | ||
Date.Formatted == "2007-02-02" | Date.Formatted == "2007-02-01") | ||
|
||
save(dat.for.2days, | ||
file = "./data/dat.for.2days.RData") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#################################################################################################### | ||
# | ||
#+ Dataset download details are provided in the "Data_Download.md" | ||
#+ Downloaded dataset is available in the folder "data" | ||
#+ The downloaded dataset has been read, formatted, filtered and saved using "format.And.Filter.R" | ||
#+ The filtered dataset is saved to the directory "data" ("dat.for.2days.RData") | ||
# | ||
#################################################################################################### | ||
|
||
#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) | ||
load(file = "./data/dat.for.2days.RData") | ||
ls() | ||
|
||
#+ plot-1 | ||
png(filename = "plot1.png", | ||
width = 480, height = 480, units = "px") | ||
hist(dat.for.2days$Global_active_power, | ||
main="Global Active Power", | ||
xlab="Global Active Power (kilowatts)", | ||
col="red" ) | ||
dev.off() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#################################################################################################### | ||
# | ||
#+ Dataset download details are provided in the "Data_Download.md" | ||
#+ Downloaded dataset is available in the folder "data" | ||
#+ The downloaded dataset has been read, formatted, filtered and saved using "format.And.Filter.R" | ||
#+ The filtered dataset is saved to the directory "data" ("dat.for.2days.RData") | ||
# | ||
#################################################################################################### | ||
|
||
|
||
#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) | ||
load(file = "./data/dat.for.2days.RData") | ||
ls() | ||
|
||
#+ plot-2 | ||
png(filename = "plot2.png", | ||
width = 480, height = 480, units = "px") | ||
with (dat.for.2days, | ||
plot(datetime, Global_active_power, type="l", xlab="", | ||
ylab="Global Active Power (kilowatts)", main="") | ||
) | ||
dev.off() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#################################################################################################### | ||
# | ||
#+ Dataset download details are provided in the Data_Download.md | ||
#+ Downloaded dataset is available in the folder "data" | ||
#+ The downloaded dataset has been read, formatted, filtered and saved using format.And.Filter.R | ||
#+ The filtered dataset is saved to the directory "data" and is named "dat.for.2days.RData" | ||
# | ||
#################################################################################################### | ||
|
||
#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) | ||
load(file = "./data/dat.for.2days.RData") | ||
ls() | ||
|
||
#+ plot-3 | ||
png( | ||
filename = "plot3.png", | ||
width = 480, height = 480, units = "px" | ||
) | ||
|
||
with ( | ||
dat.for.2days, | ||
plot(datetime, Sub_metering_1, type="l", col="black", xlab="", | ||
ylab="Energy sub metering", main="") | ||
) | ||
|
||
lines ( | ||
dat.for.2days$datetime, | ||
dat.for.2days$Sub_metering_2, | ||
col="red", xlab="", | ||
ylab="", main="" | ||
) | ||
|
||
lines ( | ||
dat.for.2days$datetime, | ||
dat.for.2days$Sub_metering_3, | ||
col="blue", xlab="", | ||
ylab="", main="" | ||
) | ||
|
||
# Add legend | ||
legend( | ||
"topright", | ||
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), | ||
col=c("black", "red", "blue"), lty=1 | ||
) | ||
|
||
|
||
dev.off() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#################################################################################################### | ||
# | ||
#+ Dataset download details are provided in the Data_Download.md | ||
#+ Downloaded dataset is available in the folder "data" | ||
#+ The downloaded dataset has been read, formatted, filtered and saved using format.And.Filter.R | ||
#+ The filtered dataset is saved to the directory "data" and is named "dat.for.2days.RData" | ||
# | ||
#################################################################################################### | ||
|
||
|
||
#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) | ||
load(file = "./data/dat.for.2days.RData") | ||
ls() | ||
|
||
#+ plot-4 | ||
png(filename = "plot4.png", | ||
width = 480, height = 480, units = "px") | ||
|
||
par(mfrow = c(2, 2)) | ||
|
||
################## | ||
# | ||
#+plot-4.1 | ||
# | ||
################## | ||
with (dat.for.2days, | ||
plot(datetime, Global_active_power, type="l", xlab="", | ||
ylab="Global Active Power", main="" | ||
) | ||
) | ||
|
||
################## | ||
# | ||
#plot-4.2 | ||
# | ||
################## | ||
with (dat.for.2days, | ||
plot( | ||
datetime, Voltage, type="l", | ||
ylab="Voltage", | ||
main="" | ||
) | ||
) | ||
|
||
################## | ||
# | ||
#plot-4.3 | ||
# | ||
################## | ||
with ( | ||
dat.for.2days, | ||
plot(datetime, Sub_metering_1, type="l", col="black", xlab="", | ||
ylab="Energy sub metering", main="") | ||
) | ||
|
||
lines ( | ||
dat.for.2days$datetime, | ||
dat.for.2days$Sub_metering_2, | ||
col="red", xlab="", | ||
ylab="", main="" | ||
) | ||
|
||
lines ( | ||
dat.for.2days$datetime, | ||
dat.for.2days$Sub_metering_3, | ||
col="blue", xlab="", | ||
ylab="", main="" | ||
) | ||
|
||
# Add legend | ||
legend( | ||
"topright", | ||
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), | ||
col=c("black", "red", "blue"), lty=1 | ||
) | ||
|
||
|
||
|
||
################## | ||
# | ||
#plot-4.4 | ||
# | ||
################## | ||
with (dat.for.2days, | ||
plot( | ||
datetime, Global_reactive_power, type="l", | ||
main="" | ||
) | ||
) | ||
|
||
|
||
dev.off() |