forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
34 lines (27 loc) · 1.51 KB
/
plot3.R
File metadata and controls
34 lines (27 loc) · 1.51 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
library(sqldf)
## setwd
#wd <- "##Path to working directory"
#curwd <- getwd()
#if ( wd != curwd ) {
# setwd(wd)
#}
#######################################
## TXT file was too big for GitHub so I only added the ZIP. This line does some
## initial setup to unzip the file and place it in the ./data directory before proceeding.
unzip("./data/household_power_consumption.txt.zip", files="household_power_consumption.txt", exdir="./data")
## read table
cols <- c("character", "character", rep("numeric", 7))
sql <- "SELECT * FROM file WHERE Date='1/2/2007' OR Date='2/2/2007'"
householdData <- read.csv.sql("./data/household_power_consumption.txt", sql = sql, header = TRUE, sep=";", colClasses = cols)
dataSample <- householdData
dateTime <- paste( dataSample$Date, dataSample$Time, sep = " " )
dateTime <- strptime( dateTime, format = "%d/%m/%Y %H:%M:%S" )
dataSample <- cbind( dataSample, dateTime )
names(dataSample) <- colNames <- c("Date", "Time", "GlobalActivePower", "GlobalReactivePower", "Voltage", "GlobalIntensity", "SubMetering1", "SubMetering2", "SubMetering3", "DateTime")
## Create plot3.png
png("plot3.png", width=480, height=480, bg="transparent")
plot(dataSample$DateTime, dataSample$SubMetering1, type="l", xlab="", ylab="Energy sub metering")
lines(dataSample$DateTime, dataSample$SubMetering2, type="l", col="red")
lines(dataSample$DateTime, dataSample$SubMetering3, type="l", col="blue")
legend("topright", legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lwd="1", col=c("black", "red", "blue"))
dev.off()