Repository navigation
Expand file tree
/
Copy pathplot4.R
More file actions
36 lines (35 loc) · 1.57 KB
/
Copy pathplot4.R
File metadata and controls
36 lines (35 loc) · 1.57 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
library('sqldf')
pwrfile <- 'household_power_consumption.txt'
if (!file.exists(pwrfile)) {
#download zip file
download.file('https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip','household_power_consumption.zip')
# unzip file into household_power_consumption.txt
unzip('household_power_consumption.zip')
}
#open file connection
pf <- file(pwrfile)
#use sqldf() to read in the dates from the file connection
#using SQL queries and create subsetted dataframe df
data <- sqldf("select * from pf where Date == '1/2/2007' or Date == '2/2/2007' ",
file.format = list(header = TRUE, sep = ";" ))
#close connection
close(pf)
#combind and convert date and time fields
data[['dateTime']] <- strptime(paste(data[['Date']],data[['Time']], sep=" "), "%d/%m/%Y %H:%M:%S")
png("plot4.png")
#config device
par(mfrow = c(2,2))
par(mar = c(4,4,2,2))
#plot data
#plot 1
with(data, plot(dateTime,Global_active_power, type = "l", xlab = "", ylab = 'Global Active Power'))
#plot 2
with(data, plot(dateTime,Voltage, type = "l", xlab = "datetime", ylab = 'Voltage'))
#plot 3(sub metering)
with(data, plot(dateTime,Sub_metering_1, type = "l", xlab = "", ylab = 'Energy sub metering'))
with(data, lines(dateTime,Sub_metering_2,col = "red"))
with(data, lines(dateTime,Sub_metering_3,col = "blue"))
legend("topright", lty=c(1,1,1),lwd=c(2.5,2.5,2.5),col = c("black","red","blue"), legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
#plot 4
with(data, plot(dateTime,Global_reactive_power, type = "l", xlab = "datetime", ylab = 'Global_reactive_power'))
dev.off()