-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
44 lines (31 loc) · 2.32 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
## Download the household power consumption data from UC irvine machine learning repository
## save the file in the project_1 folder
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip","./project_1/household_power_consumption.zip")
## unzip the data
unzip("./project_1/household_power_consumption.zip", files = NULL,exdir = "./project_1",unzip = "internal")
## Delete the zip file to keep the project space clean
unlink("./project_1/household_power_consumption.zip")
## read the data into R
## note the seperator in this file is a ";" not a comma
hh_power_consumption <- read.table("./project_1/household_power_consumption.txt",header = TRUE, sep = ";", stringsAsFactors = FALSE, dec = ".")
## create a subset of data per the project requirements. The subset will inlcude data from 2007-02-01 and 2007-02-02
working_data <- hh_power_consumption[hh_power_consumption$Date %in% c("1/2/2007","2/2/2007") ,]
## convert the time and date columns to POSIX format
datetime_data <- strptime(paste(working_data$Date, working_data$Time, sep = " "),"%d/%m/%Y %H:%M:%S")
## to create a png set the device output to png and make the size 480x480
png("./project_1/plot4.png", width = 480,height = 480)
## set the window to create a 2 by 2 display of the plots
par(mfrow = c(2,2))
## create the active power plot
plot(datetime_data, as.numeric(working_data$Global_active_power), type = "l", xlab = "", ylab = "Global Active Power", cex = 0.2)
## create the voltage plot
plot(datetime_data, as.numeric(working_data$Voltage), type = "l", xlab = "datetime", ylab = "Voltage")
## create the submetering plot
plot(datetime_data, as.numeric(working_data$Sub_metering_1), type = "l", ylab = "Energy Submetering", xlab = "")
lines(datetime_data, as.numeric(working_data$Sub_metering_2), type = "l", col = "red")
lines(datetime_data, as.numeric(working_data$Sub_metering_3), type = "l", col = "blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty = , lwd = 2.5, col = c("black", "red", "blue"), bty = "o")
##create the reactive power plot
plot(datetime_data, as.numeric(working_data$Global_reactive_power), type = "l", xlab = "datetime", ylab = "Global_reactive_power")
## close the png device output to save the file and return output to screen
dev.off()