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
75 lines (60 loc) · 3.1 KB
/
Copy pathplot4.R
File metadata and controls
75 lines (60 loc) · 3.1 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
##Ensure that your working directory is set to an empty folder in your desired
##location before running plot1()
plot4 <- function(){
#load required sqldf library. sqldf is used to subset the desired
##days from large data file when loading the data
library(sqldf)
##checks for data file and unzips it
if(!file.exists("household_power_consumption.txt")){
##Data source
dataURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
##name downloaded file
zipName <- "PowerConsumption.zip"
##download the file from the source
download.file(dataURL, zipName, mode = "wb")
##Unzip the file into the set working directory
unzip(zipName)
}
##Assuming data is in a text file retrieves the name of the data file.
##if the data is stored in a different file type this code must be modified
files <- list.files()
file <- grep(pattern = "\\.txt", files, value = TRUE)
##Reads the data using an sql statement to select only the desired days from the data
data1 <<- read.csv.sql(file, sql = "SELECT * FROM file
WHERE Date = '1/2/2007'
OR Date = '2/2/2007'", header = TRUE, sep = ";")
closeAllConnections()
##Convert columns 3 to end to numeric
numercol <- c(3:ncol(data1))
data1[,numercol] <- as.numeric(as.character(unlist(data1[,numercol])))
##paste date and time fields into single column
data1$datetime <- paste(data1$Date, data1$Time, sep=" ")
##use strptime() to convert datetime column into "POSIXlt" "POSIXt" format
data1$POSdate <- strptime(data1$datetime, format = "%d/%m/%Y %H:%M:%S")
##sets graphic device to plot2.png output in repo folder - 480x480 pixels with transparent background
png('./repo/plot4.png',width=480,height=480,units="px",bg = "transparent")
##creates 2 row and 2 column graphic device
par(mfrow = c(2, 2))
##top left plot##
plot(data1$POSdate,data1$Global_active_power, type = "n", ylab = "Global Active Power", xlab = "")
##plots line of the Global Active Power vs Date
lines(data1$POSdate,data1$Global_active_power)
##top right plot##
plot(data1$POSdate,data1$Voltage, type = "n", ylab = "Voltage", xlab = "datetime")
##plots line of Voltage vs Date
lines(data1$POSdate,data1$Voltage)
##bottom left plot##
plot(data1$POSdate, data1$Sub_metering_1, type = "n", ylab = "Energy sub metering", xlab = "")
##plots lines of each sub-meter vs Date
lines(data1$POSdate, data1$Sub_metering_1, col = "black")
lines(data1$POSdate, data1$Sub_metering_2, col = "red")
lines(data1$POSdate, data1$Sub_metering_3, col = "blue")
##adds legend on top right with lines and color coded
legend(x="topright", lty = 1, legend = c(names(data1[,7:9])), col = c("black", "red", "blue"), bty="n")
##bottom right plot##
plot(data1$POSdate,data1$Global_reactive_power, type = "n", ylab = "Global_reactive_power", xlab = "datetime")
##plots line of Voltage vs Date
lines(data1$POSdate,data1$Global_reactive_power)
##close connection to png
dev.off()
}