forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
76 lines (62 loc) · 2.82 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
###############################################
### plot4.R ###
### Author: Lizel Greyling ###
### 3 December 2014 ###
###############################################
# This script loads and cleans the data form the household_power_consumption.txt file and
# converts the date and time fields into useable DateTime format.
# The script creates four different plots on a sigle plane and saves it as a PNG file in
# the working directory.
# Load required packages:
library(sqldf)
# The sqldf package allows sql to be used to only load a subset of the data.
library(lubridate)
# The lubridate package makes working with dates and times easier.
# A function to import and clean the data:
getdata <- function() {
# The data must be downloaded and unzipped in the working directory.
# Download the data from https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
f <- "household_power_consumption.txt"
# Set the SQL command used to select only the correct rowns (where the date is
# either 2007-02-01 or 2007-02-02):
sqlstring <- "SELECT * FROM file WHERE Date = '1/2/2007' OR Date = '2/2/2007'"
# Read only the rows with the correct dates:
nrg <- read.csv.sql(f, sqlstring, sep = ";")
# Fix date and time fields:
# Put date and time in the same field:
nrg$DateTime <- paste(nrg$Date,nrg$Time)
# Convert text field DateTime to POSIXct:
nrg$DateTime <- dmy_hms(nrg$DateTime)
return(nrg)
}
# Test whether the dataframe is already in memory and cleaned (date & time fixed)
# and only call the getdata function if it's not:
if (!exists("nrg")) {
nrg <- getdata() # calls getdata() because the dataframe is not in memory
} else
{
if (class(nrg$DateTime)[1] != "POSIXct") {
nrg <- getdata() # calls getdata() because although the dataset is in memory, the date/time is wrong
}
}
# open png device and set parameters:
png(filename="plot4.png",width = 480, height = 480)
#setup graph layout:
par(mfcol = c(2,2))
# set graphs:
# Graph 1, top left:
plot(x=nrg$DateTime,y=nrg$Global_active_power,
type="l", xlab = "", ylab = "Global Active Power")
# Graph 2, bottom left:
plot(x=nrg$DateTime,y=nrg$Sub_metering_1,
type="l", xlab = "", ylab = "Energy sub metering")
lines(x=nrg$DateTime,y=nrg$Sub_metering_2,col="red", type="l")
lines(x=nrg$DateTime,y=nrg$Sub_metering_3,col="blue", type="l")
legend("topright", col = c("black","red","blue"),lty=1, bty = "n",
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
# Graph 3, top right:
plot(x=nrg$DateTime,y=nrg$Voltage, type="l", xlab = "datetime", ylab = "Voltage")
# Graph 4, bottom right:
plot(x=nrg$DateTime,y=nrg$Global_reactive_power, type="l", xlab="datetime", ylab = "Global_reactive_power")
# close png device:
dev.off()