forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
51 lines (43 loc) · 2.11 KB
/
plot3.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
## plot3 of project 1 for Exploratory Data Analysis
## file path of data
power_file <- "household_power_consumption.txt"
## Cache data - The following if statement checks to see if data is already loaded into
## global environment. If not, it will read data and assign to power_data.
## comment out next line once data has been loaded into global envir
power_data <- NULL
if(is.null(power_data)){
## return row number of where to start reading data, date Feb 1 2007
start <- grep(as.character("1/2/2007"), readLines(power_file), fixed = T)[1] - 1
## return row number of where to end reading data, date Feb 2 2007
end <- grep(as.character("3/2/2007"), readLines(power_file), fixed = T)[1] - 1
## return number of rows to read
diff <- end - start
## extract header from data
header <- read.table(power_file, sep = ";", nrows = 1, header = T)
## read specified data into data frame called power_Data
power_data <- read.table(power_file,
sep = ";",
skip = start,
nrows = diff,
na.strings = "?")
## assign header to power_data
colnames(power_data) <- colnames(header)
## combine date and time data and assign to new column of power_data
power_data$new.col <- as.POSIXct(paste(power_data$Date, power_data$Time),
format="%d/%m/%Y %H:%M:%S")
}
## plot Sub Metering 1, 2, and 3 for Date/Time
plot(power_data$new.col, power_data$Sub_metering_1, type = "l",
ylab = "Energy sub metering", xlab = "")
lines(power_data$new.col, power_data$Sub_metering_2, col = "red")
lines(power_data$new.col, power_data$Sub_metering_3, col = "blue")
## Add legend
legend("topright",
lty = 1,
col = c("black", "red", "blue"),
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
text.width = strwidth(" Sub_metering_1 ")
)
## copy plot to png file
dev.copy(png, file = "plot3.png", width = 480, height = 480)
dev.off()