Skip to content

Commit 943161a

Browse files
author
Carlos E Hernández R
committed
ready for submit
1 parent 73fe5c6 commit 943161a

12 files changed

+165
-57
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
household_power_consumption.txt
6+
figure/

ExData_Plotting1.Rproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

README.md

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Introduction
22

3-
This assignment uses data from
3+
This project uses data from
44
the <a href="http://archive.ics.uci.edu/ml/">UC Irvine Machine
55
Learning Repository</a>, a popular repository for machine learning
66
datasets. In particular, we will be using the "Individual household
@@ -33,82 +33,43 @@ web site</a>:
3333
<li><b>Sub_metering_3</b>: energy sub-metering No. 3 (in watt-hour of active energy). It corresponds to an electric water-heater and an air-conditioner.</li>
3434
</ol>
3535

36-
## Loading the data
37-
38-
39-
40-
41-
42-
When loading the dataset into R, please consider the following:
43-
44-
* The dataset has 2,075,259 rows and 9 columns. First
45-
calculate a rough estimate of how much memory the dataset will require
46-
in memory before reading into R. Make sure your computer has enough
47-
memory (most modern computers should be fine).
48-
49-
* We will only be using data from the dates 2007-02-01 and
50-
2007-02-02. One alternative is to read the data from just those dates
51-
rather than reading in the entire dataset and subsetting to those
52-
dates.
53-
54-
* You may find it useful to convert the Date and Time variables to
55-
Date/Time classes in R using the `strptime()` and `as.Date()`
56-
functions.
57-
58-
* Note that in this dataset missing values are coded as `?`.
36+
## Quick Start
5937

38+
| Step | Example |
39+
| ------------- | ------------- |
40+
| Clone this project | ```git clone https://github.com/carlosehernandezr/ExData_Plotting1.git``` |
41+
| Download the dataset and unzip the file on the project folder | https://archive.ics.uci.edu/ml/datasets/Individual+household+electric+power+consumption |
42+
| Run the script of the plot that you want |
6043

6144
## Making Plots
6245

6346
Our overall goal here is simply to examine how household energy usage
64-
varies over a 2-day period in February, 2007. Your task is to
65-
reconstruct the following plots below, all of which were constructed
66-
using the base plotting system.
67-
68-
First you will need to fork and clone the following GitHub repository:
69-
[https://github.com/rdpeng/ExData_Plotting1](https://github.com/rdpeng/ExData_Plotting1)
70-
71-
72-
For each plot you should
73-
74-
* Construct the plot and save it to a PNG file with a width of 480
75-
pixels and a height of 480 pixels.
76-
77-
* Name each of the plot files as `plot1.png`, `plot2.png`, etc.
78-
79-
* Create a separate R code file (`plot1.R`, `plot2.R`, etc.) that
80-
constructs the corresponding plot, i.e. code in `plot1.R` constructs
81-
the `plot1.png` plot. Your code file **should include code for reading
82-
the data** so that the plot can be fully reproduced. You should also
83-
include the code that creates the PNG file.
84-
85-
* Add the PNG file and R code file to your git repository
86-
87-
When you are finished with the assignment, push your git repository to
88-
GitHub so that the GitHub version of your repository is up to
89-
date. There should be four PNG files and four R code files.
90-
91-
92-
The four plots that you will need to construct are shown below.
47+
varies over a 2-day period in February, 2007 and reconstruct the following plots below,
48+
all of which were constructed using the base plotting system.
9349

50+
The four plots that you can construct are shown below.
9451

9552
### Plot 1
9653

9754

98-
![plot of chunk unnamed-chunk-2](figure/unnamed-chunk-2.png)
55+
![plot 1](plot1.png)
9956

10057

10158
### Plot 2
10259

103-
![plot of chunk unnamed-chunk-3](figure/unnamed-chunk-3.png)
60+
![plot 2](plot2.png)
10461

10562

10663
### Plot 3
10764

108-
![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-4.png)
65+
![plot 3](plot3.png)
10966

11067

11168
### Plot 4
11269

113-
![plot of chunk unnamed-chunk-5](figure/unnamed-chunk-5.png)
70+
![plot 4](plot1.png)
71+
72+
## Dependecies
11473

74+
This project use the **lubridate** package, you can download from **CRAN** just like that:
75+
`install.packages('lubridate')`

functions.R

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#set english as locale
2+
Sys.setlocale(category = "LC_ALL", locale = "english")
3+
4+
#' this function read the data set and parse it to data frame format
5+
#' @param fileName the name of file that contains the data set, as default 'household_power_consumption'
6+
#' @param from the date form start the read, as default '2007-02-01'
7+
#' @param to the date to end the read '2007-02-02
8+
read_data <- function(fileName ="household_power_consumption", from = "2007-02-01", to ="2007-02-02"){
9+
10+
library(lubridate)
11+
12+
print("Reading file")
13+
data <- read.table(paste0(fileName,".txt"), sep = ";", header = TRUE,
14+
colClasses = c("character","character","double","double","double","double","double","double","double"),
15+
na.strings = c("?"), stringsAsFactors = FALSE)
16+
17+
print("Formatting date and time")
18+
data$Date = parse_date_time(data$Date, orders = c("ymd", "dmy", "mdy"))
19+
data$Time = as.POSIXct(paste(data$Date, data$Time), format="%Y-%m-%d %H:%M:%S")
20+
21+
print("Subsetting, by default: from the dates 2007-02-01 and 2007-02-02")
22+
df <- data[data$Date >= as.Date(from) & data$Date <= as.Date(to),]
23+
24+
print("Finished")
25+
26+
return(df)
27+
}
28+
29+
#' this function draw the plot1
30+
#' @param data the data set
31+
#' @param fileName the name for the png file, as default 'plot1'
32+
#' @param saveAsPng if is TRUE save the plot on a png file else only show the plot, as default FALSE
33+
#' @param width the height of the png file, as default 480px
34+
#' @param height the width of the png file, as default 480px
35+
drawPlot1 <- function(data, fileName="plot1", saveAsPng = FALSE, width = 480, height = 480){
36+
37+
if(saveAsPng){
38+
png(paste0(fileName,".png"), width, height)
39+
}
40+
41+
hist(data$Global_active_power,col="green", xlab = "Global Active Power (kilowatts)", main = "Global Active Power")
42+
43+
if(saveAsPng){
44+
dev.off()
45+
}
46+
}
47+
#' this function draw the plot2
48+
#' @param data the data set
49+
#' @param fileName the name for the png file, as default 'plot2'
50+
#' @param saveAsPng if is TRUE save the plot on a png file else only show the plot, as default FALSE
51+
#' @param width the height of the png file, as default 480px
52+
#' @param height the width of the png file, as default 480px
53+
drawPlot2 <- function(data, fileName="plot2", saveAsPng = FALSE, width = 480, height = 480){
54+
if(saveAsPng){
55+
png(paste0(fileName,".png"), width, height)
56+
}
57+
plot(data$Time, data$Global_active_power, type = "l", ylab = "Global Active Power (kilowatts)", xlab = "")
58+
59+
if(saveAsPng){
60+
dev.off()
61+
}
62+
}
63+
#' this function draw the plot3
64+
#' @param data the data set
65+
#' @param fileName the name for the png file, as default 'plot3'
66+
#' @param saveAsPng if is TRUE save the plot on a png file else only show the plot, as default FALSE
67+
#' @param width the height of the png file, as default 480px
68+
#' @param height the width of the png file, as default 480px
69+
drawPlot3 <- function(data, fileName="plot3", saveAsPng = FALSE, width = 480, height = 480){
70+
if(saveAsPng){
71+
png(paste0(fileName,".png"), width, height)
72+
}
73+
plot(data$Time, data$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering")
74+
lines(data$Time, data$Sub_metering_2, type = "l",col="2")
75+
lines(data$Time, data$Sub_metering_3, type = "l",col="3")
76+
legend("topright", legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col=1:3, lty=1, cex=0.8)
77+
78+
if(saveAsPng){
79+
dev.off()
80+
}
81+
}
82+
#' this function draw the plot4
83+
#' @param data the data set
84+
#' @param fileName the name for the png file, as default 'plot4'
85+
#' @param saveAsPng if is TRUE save the plot on a png file else only show the plot, as default FALSE
86+
#' @param width the height of the png file, as default 480px
87+
#' @param height the width of the png file, as default 480px
88+
drawPlot4 <- function(data, fileName="plot4", saveAsPng = FALSE, width = 480, height = 480){
89+
90+
if(saveAsPng){
91+
png(paste0(fileName,".png"), width, height)
92+
}
93+
94+
par(mfrow = c(2,2), mar = c(4, 4, 2, 1))
95+
drawPlot2(data)
96+
plot(data$Time, data$Voltage, type = "l", ylab = "Voltage", xlab = "datetime")
97+
drawPlot3(data)
98+
plot(data$Time, data$Global_reactive_power, type = "l", ylab = "Global Reactive Power", xlab = "datetime")
99+
100+
if(saveAsPng){
101+
dev.off()
102+
}
103+
}

plot1.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# getting data
2+
source("./functions.R")
3+
data <- read_data()
4+
5+
#for the code source please look at fuctions.R
6+
drawPlot1(data, saveAsPng = TRUE)

plot1.png

3.65 KB
Loading

plot2.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# getting data
2+
source("./functions.R")
3+
data <- read_data()
4+
5+
#for the code source please look at fuctions.R
6+
drawPlot2(data, saveAsPng = TRUE)

plot2.png

4.4 KB
Loading

plot3.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# getting data
2+
source("./functions.R")
3+
data <- read_data()
4+
5+
#for the code source please look at fuctions.R
6+
drawPlot3(data, saveAsPng = TRUE)

plot3.png

3.75 KB
Loading

plot4.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# getting data
2+
source("./functions.R")
3+
data <- read_data()
4+
5+
#for the code source please look at fuctions.R
6+
drawPlot4(data, saveAsPng = TRUE)
7+

plot4.png

7.54 KB
Loading

0 commit comments

Comments
 (0)