Repository navigation
Expand file tree
/
Copy pathplot3.R
More file actions
122 lines (91 loc) · 4.18 KB
/
Copy pathplot3.R
File metadata and controls
122 lines (91 loc) · 4.18 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
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
## Plot line graph of energy sub meterings vs datetimes
## Assumes household_power_consumption.txt is in the current work folder.
##
## ARGUMENTS:
## file: filepath to txt holding the data.
##
## device: graphical output device (default to png). The only other accetable
## value is 'display'
##
## data: optional argument to pass in formatted dataframe object for the plot
plot3 <- function(file = 'household_power_consumption.txt', device='png',
data = NULL) {
# Date when data was downloaded if the internal download routine was used.
file_name <- 'Existing data used.'
# check if file exists. If it doesn't, try default. If still fails,
# download & extract, then set file back to default.
if (!file.exists(file)) {
if (!file.exists('household_power_consumption.txt')) {
# Set file name to the download date.
file_name <- paste(as.Date(Sys.time()), '.zip')
download.file('https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip',
destfile = file_name)
unzip(file_name)
}
file <- 'household_power_consumption.txt'
}
## Data parser to read the Electric power consumption data
## Hardcoded to start from the first entry on 2007-02-01 and stop at last
## entry for 2007-02-02.
##
##
## ARGUMENTS
## file: string indicating file path to the txt file holding the data.
##
## RETURNS
## data: parsed data stored in a table
data_parser <- function(file = 'household_power_consumption.txt') {
# Compute number of lines that must be skipped.
txt <- readLines(file)
# grep matching date string using regexp for both 2007-02-01 and 2007-02-02
grep_result <- grep('^[12]/2/2007', txt)
# number of lines to skip is right before the first matched index
skip_num <- grep_result[1] - 1
# number of lines to read is just the number of index matched
nrows <- length(grep_result)
# Only really start reading when the line starts with the starting date.
raw_data <- read.table(file, skip = skip_num, nrows = nrows, header = FALSE,
sep = ';', na.strings = '?')
# add the column names
colnames(raw_data) <- names(read.table(file, nrows = 1, header = TRUE,
sep = ';'))
# Create time stamps from raw_data using datetime objects
Timestamps <- strptime(mapply(paste, raw_data$Date, raw_data$Time,
MoreArgs = list(sep = ' '), SIMPLIFY = TRUE),
format = '%d/%m/%Y %H:%M:%S', tz = 'GMT')
# Create new data frame with R datetime objects within range
data <- data.frame(Timestamps, raw_data[3:length(raw_data)])
}
if (device == 'png' || device == 'display') {
if (is.null(data)) {
# Parse data
data <- data_parser(file)
}
if (device == 'png') {
# open PNG device
png('plot3.png')
}
# Plot line graphs
# color array
cols <- c('black', 'red', 'blue')
# submeter 1 and set up y label
plot(data$Timestamps, data$Sub_metering_1, type = 'l', col = cols[1],
ylab = 'Energy sub metering', xlab = '')
# submeter 2
lines(data$Timestamps, data$Sub_metering_2, type = 'l', col = cols[2])
# submeter 3
lines(data$Timestamps, data$Sub_metering_3, type = 'l', col = cols[3])
# add legend
legend('topright', legend =
names(data)[(length(names(data)) - 2):length(names(data))],
col = cols, lty = 1)
if (device == 'png') {
# save and close device
dev.off()
}
} else {
stop('invalid graphic device')
}
# Return name of the current data zip or mention existing data used.
file_name
}