forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot4.R
93 lines (66 loc) · 3.76 KB
/
Plot4.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
## Step 1 - load the required libraries (and install if not there)
###############################################################################
## no pre-requisites
## Step 2 - Check if files already downloaded / unzipped, if not go get it
###############################################################################
## setting a variable for the unzipped file directory to re-use
fileDirectory = "./PowerData/"
zipFileDownloadLocation = "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zippedFile = "exdata%2Fdata%2Fhousehold_power_consumption.zip"
unzippedFile = "household_power_consumption.txt"
if(!file.exists(fileDirectory)){
dir.create(fileDirectory)
download.file(zipFileDownloadLocation,
paste0(fileDirectory, zippedFile), method = "curl")
unzip(paste0(fileDirectory, zippedFile),
unzip = "internal", exdir = fileDirectory)
## clean up - remove the temp zip file....
file.remove(paste0(fileDirectory, zippedFile))
}
## Step 3 - Read in power data
###############################################################################
## Deliberately setting date and time columns as character at the moment to do
## the initial filter of the file import - converted to proper formats later.
powerData <- read.table(paste0(fileDirectory, unzippedFile),
stringsAsFactors = FALSE, header = TRUE, sep = ";" ,
na.strings = "?",
colClasses = c("character","character","numeric",
"numeric","numeric","numeric","numeric",
"numeric","numeric"))
## Step 4 - Format the data
###############################################################################
## Format of the date in the file is %d/%m/%Y without leading zeros...
## to make it easy to subset I have left "Date" field as character...
dateLimits <- c("1/2/2007", "2/2/2007")
powerDataSubset <- subset(powerData, Date %in% dateLimits)
## Step 5 - Reformat the data ready for plotting
###############################################################################
## ... then convert Separate Date and Time Columns to a single datetime
## need to wrap the data / time conversion in as.POSIXlt as otherwise is
## being saved as character
powerDataSubset$datetime <- as.POSIXlt(strftime(paste(
as.Date(powerDataSubset$Date, "%d/%m/%Y"),powerDataSubset$Time),tz = ""))
powerDataSubset <- subset(powerDataSubset, select = c(10,3:9))
## Step 6 - Create the plot
###############################################################################
## Set Up the Plot Area
par(mfcol = c(2,2))
## Tweaked Version of Plot 2 (will go in top left)
plot(powerDataSubset$datetime,powerDataSubset$Global_active_power, type = "l",
ylab = "Global Active Power", xlab = "")
## Version of Plot 3 (will go bottom left)
with(powerDataSubset, plot(datetime,Sub_metering_1 , type = "l",
ylab = "Energy sub metering", xlab = ""))
with(powerDataSubset, points(datetime ,Sub_metering_2 , type = "l" ,col= "red"))
with(powerDataSubset, points(datetime ,Sub_metering_3 , type = "l" ,col= "blue"))
legtext <- names(powerDataSubset[6:8])
legcols <- c("black", "red", "blue")
legend("topright", legend = legtext, lty= 1, col = legcols)
## 3rd Plot (will go in top right)
with(powerDataSubset, plot(datetime,Voltage , type = "l"))
## 4th Plot (will go in bottom right)
with(powerDataSubset, plot(datetime ,Global_reactive_power , type = "l"))
## Step 7 - Export the Plot as PNG
###############################################################################
dev.copy(png, "Plot4.png")
dev.off()