forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
45 lines (36 loc) · 1.57 KB
/
plot1.R
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## PLOT 1
print("--- PLOT 1 ---")
## FILE Locations
fileZip <- "household_power_consumption.zip"
fileTxt <- "household_power_consumption.txt"
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## DOWNLOAD
print("DOWNLOADING ...")
if (!(file.exists(fileZip))) {
download.file(fileUrl, fileZip, "curl", quiet = FALSE, mode = "wb", cacheOK = TRUE)
}
## UNZIP
print("UNZIPING ...")
if (!(file.exists(fileTxt))) {
unzip(fileZip, overwrite = TRUE, junkpaths = TRUE, exdir = ".")
download.file(fileUrl, fileZip, "curl", quiet = FALSE, mode = "wb", cacheOK = TRUE)
}
## CSV Decoding Preparation
setClass("myDate")
setAs("character","myDate", function(from) as.Date(from, format="%d/%m/%Y"))
csvCols <- c("myDate","character","numeric","numeric","numeric","numeric","numeric","numeric","numeric")
## READ DATA, NAs are ? and ; as Seperators
print("READING CSV ...")
data <- read.csv("household_power_consumption.txt", sep = ";", dec=".", colClasses = csvCols, na.strings ="?")
print("SUBSETTING Data ...")
sel1Data <- subset(data, Date == "2007-02-01")
sel2Data <- subset(data, Date == "2007-02-02")
selData <- rbind(sel1Data,sel2Data)
print("PLOTTING ...")
png(file = "plot1.png", width = 480, height = 480, units = "px", bg = "transparent")
with(selData, hist(Global_active_power, col = "red", main = "Global Active Power",
xlab = "Global Active Power (kilowatts)",
ylab = "Frequency"))
print("SAVING PNG ...")
##dev.copy(png, file = "plot1.png", width = 480, height = 480, units = "px", bg = "transparent")
dev.off()