Skip to content

Commit

Permalink
Added plots
Browse files Browse the repository at this point in the history
  • Loading branch information
nairps committed Jun 24, 2019
1 parent 73fe5c6 commit 86b3820
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Data_Download.md
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 added data/dat.for.2days.RData
Binary file not shown.
Binary file added data/household_power_consumption.zip
Binary file not shown.
35 changes: 35 additions & 0 deletions format.And.Filter.R
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")

22 changes: 22 additions & 0 deletions plot1.R
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()

Binary file added plot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions plot2.R
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()

Binary file added plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions plot3.R
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()


Binary file added plot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions plot4.R
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()
Binary file added plot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 86b3820

Please sign in to comment.