forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
50 lines (39 loc) · 1.45 KB
/
plot4.R
File metadata and controls
50 lines (39 loc) · 1.45 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
LoadAndCleanData <- function() {
#Load data into memory
dat <- read.table("household_power_consumption.txt",
sep=";", header=T, na.strings=c("?"))
#Append datetime field based on Date and Time fields
dat$datetime <- strptime(paste(dat$Date, dat$Time),
format="%d/%m/%Y%H:%M:%S")
#Reduce dataset to dates of interest
dat <- subset(dat, as.Date(datetime) >= as.Date("2007-02-01"))
dat <- subset(dat, as.Date(datetime) <= as.Date("2007-02-02"))
dat
}
producePlot4 <- function(dat) {
#Produce 2 by 2 plot
tmp <- producePlot3(dat)
par(mfrow = c(2,2), oma=c(2,0,0,0))
#top-left plot
yAxisText <- "Global Active Power (kilowatts)"
with(dat, {
plot(datetime, Global_active_power, xlab="", ylab=yAxisText, type="n")
lines(datetime, Global_active_power)
})
#top-right plot
with(dat, plot(datetime, Voltage, type="l"))
#bottom-left plot
yAxisText <- "Energy sub metering"
with(dat, {
plot(datetime, Sub_metering_1, xlab="", ylab=yAxisText, type="n")
lines(datetime, Sub_metering_1)
lines(datetime, Sub_metering_2, col="red")
lines(datetime, Sub_metering_3, col="blue")
})
#bottom-right plot
with(dat, plot(datetime, Global_reactive_power, type="l"))
}
#Run script.
#First produce clean data, then produce plot.
dat <- LoadAndCleanData()
producePlot4(dat)