forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot1.R
More file actions
31 lines (28 loc) · 1.48 KB
/
plot1.R
File metadata and controls
31 lines (28 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Author: Carlos Barboza
# Date: 2015-01-10
# Coursera Exploratory Data Analysis Course, JHS
#
# This scripts creates an histogram of the Global Active Power samples from the 1st and 2nd day of
# February 2007.
#
# It looks for the filtered set of measurements (filtered_power_consumption.csv) on the working directory.
# If the file is not found, it calls the filterByDays function to create this data set from the original one.
# After that it creates a png file with the appropriate size and labels on the working directory.
#
# In order to work properly, the script must be on the same directory as the filter_by_days.R and
# household_power_consumption.txt files.
# source script to filter the data on the required days
source("filter_by_days.R", local=TRUE)
# if file with filtered data already exists, load it, otherwise filter the original data.
if(!file.exists("filtered_power_consumption.csv")) {
filteredData <- filterByDays("household_power_consumption.txt", c("1/2/2007","2/2/2007"))
write.table(filteredData,"filtered_power_consumption.csv", sep= ",", row.names=FALSE)
} else {
filteredData <- read.table("filtered_power_consumption.csv", sep=",", stringsAsFactors=FALSE, header=TRUE)
}
# open png graphic device to store the graph
png("plot1.png", width=480, height=480,units="px")
# creates the histogram on the graphic device
hist(filteredData$Global_active_power, col= "red", main = "Global Active Power", xlab = "Global Active Power (Kilowatts)")
# closes the graphic device
dev.off()