forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
163 lines (138 loc) · 5.31 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#----------------------------------------------------------------------------
# plot4.R
# course: Exploratory Data Analysis project# 1
#
# Our overall goal here is simply to examine how household energy usage
# varies over a 2-day period in February, 2007. Your task is to reconstruct
# the following plots below, all of which were constructed using the base
# plotting system.
#
# First you will need to fork and clone the following GitHub repository:
# https://github.com/rdpeng/ExData_Plotting1
#
# For each plot you should
#
# Construct the plot and save it to a PNG file with a width of 480 pixels
# and a height of 480 pixels.
#
# Name each of the plot files as plot1.png, plot2.png, etc.
#
# Create a separate R code file (plot1.R, plot2.R, etc.) that constructs
# the corresponding plot, i.e. code in plot1.R constructs the plot1.png plot.
# Your code file should include code for reading the data so that the plot
# can be fully reproduced. You should also include the code that creates
# the PNG file.
#
# Add the PNG file and R code file to your git repository
#
# When you are finished with the assignment, push your git repository to
# GitHub so that the GitHub version of your repository is up to date.
# There should be four PNG files and four R code files.
#
# Graph #4 should plot 4 graphs arranged in a 2x2 fashion for the dates
# between 2007-02-01 and 2007-02-02. The four graphs are:
#
# [1,1] Global Active Power against time
# [1,2] Voltage against time
# [2,1] Energy sub-metering against time
# [2,2] Global reactive power against time
#----------------------------------------------------------------------------
library(dplyr)
library(lubridate)
# Data source and destination
zipurl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
zipfile <- './exdata_data_household_power_consumption.zip'
datafile <- 'household_power_consumption.txt';
plotdatafile <- 'household_power_consumption-20070201-20070202.txt';
# download from source if file is not present already
if (!file.exists(zipfile)) {
download.file(zipurl, zipfile, 'curl')
}
# unzip the archive if not unzipped already
if (file.exists(zipfile) & !file.exists(datafile)) {
unzip(zipfile, overwrite = TRUE)
}
if (!file.exists(plotdatafile)) {
# load file as semi-colon separated values
datacsv <- read.csv(datafile, stringsAsFactors = FALSE, sep=';', na.strings='?')
na.omit(datacsv)
# Convert to data table so we can work with dplyr
data <- tbl_df(datacsv)
# Create a single column for DateTime
data <- mutate(data, Date = dmy(Date))
data <- mutate(data, DateTime = paste(Date, Time, sep=' '))
data <- mutate(data, DateTime = ymd_hms(DateTime))
# filter the data so we keep only Feb 1-2 2007
start <- ymd_hms('2007-02-01 00:00:00')
end <- ymd_hms('2007-02-02 23:59:59')
plotdata <- filter(data, (DateTime >= start) & (DateTime <= end))
# save to file so that we avoid long load time next time we run
write.csv(plotdata, file=plotdatafile, row.names = FALSE)
} else {
plotcsvdata <- read.csv(plotdatafile, stringsAsFactors = FALSE, header=TRUE)
plotdata <- tbl_df(plotcsvdata)
}
# mutate DateTime to actual POSIXct date-time objects
plotdata <- mutate(plotdata, DateTime = ymd_hms(DateTime))
# plot lines to screen as per figure provided
par(mfrow = c(2,2))
with(plotdata, {
# graph at (1,1)
plot(DateTime, Global_active_power,
type='l',
xlab = '',
ylab='Global Active Power')
# graph at (1,2)
plot(DateTime, Voltage,
type='l',
xlab = 'datetime',
ylab='Voltage')
# graph at (2,1)
plot(DateTime, Sub_metering_1, col = 'black', type='l', xlab = '', ylab='Energy sub metering')
lines(DateTime, Sub_metering_2, col = 'red')
lines(DateTime, Sub_metering_3, col = 'blue')
# add legend to graph at (2,1)
legend('topright',
bty = 'n', ## no border around legend
cex = 0.85, ## reduce font size used in legend
lwd = 1, ## use lines in legend
col = c('black', 'red', 'blue'),
legend = c('Sub_metering_1','Sub_metering_2','Sub_metering_3'))
# graph at (2,2)
plot(DateTime, Global_reactive_power,
type='l',
xlab = 'datetime',
ylab='Global_reactive_power')
})
# plot lines to PGN file as requested (same code as above)
png(file = 'plot4.png', width = 480, height= 480) ## open PNG device
par(mfrow = c(2,2))
with(plotdata, {
# graph at (1,1)
plot(DateTime, Global_active_power,
type='l',
xlab = '',
ylab='Global Active Power')
# graph at (1,2)
plot(DateTime, Voltage,
type='l',
xlab = 'datetime',
ylab='Voltage')
# graph at (2,1)
plot(DateTime, Sub_metering_1, col = 'black', type='l', xlab = '', ylab='Energy sub metering')
lines(DateTime, Sub_metering_2, col = 'red')
lines(DateTime, Sub_metering_3, col = 'blue')
# add legend to graph at (2,1)
legend('topright',
bty = 'n', ## no border around legend
cex = 0.85, ## reduce font size used in legend
lwd = 1, ## use lines in legend
col = c('black', 'red', 'blue'),
legend = c('Sub_metering_1','Sub_metering_2','Sub_metering_3'))
# graph at (2,2)
plot(DateTime, Global_reactive_power,
type='l',
xlab = 'datetime',
ylab='Global_reactive_power')
})
dev.off() ## close the PNG file device