forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
50 lines (40 loc) · 1.73 KB
/
plot2.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
46
47
48
49
50
## PLOT 2
Sys.setlocale("LC_TIME", "C")
print("--- PLOT 2 ---")
## 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)
weekdays <- strptime(paste(selData$Date, selData$Time), format='%Y-%m-%d %H:%M:%S')
selData <- cbind(selData, weekdays)
print("PLOTTING ...")
png(file = "plot2.png", width = 480, height = 480, units = "px", bg = "transparent")
with(selData, plot(weekdays, Global_active_power, type="l", col = "black", main = "",
xlab = "",
ylab = "Global Active Power (kilowatts)"
))
print("SAVING PNG ...")
##dev.copy(png, file = "plot2.png", width = 480, height = 480, units = "px", bg = "transparent")
dev.off()