forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
36 lines (32 loc) · 1.64 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
wdURL <- "C:/Users/212446591/Desktop/coursera/Assignment/4. Explatory Data Analysis/Week1/Project"
setwd(wdURL)
## Create folder UCI_Irvine_DATA in working directory
if(!file.exists("./UCIrvine_DATA")){
dir.create("./UCIrvine_DATA")
}
## download file and unzip it
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(fileUrl,destfile="./UCIrvine_DATA/Data.zip")
unzip(zipfile="./UCIrvine_DATA/Data.zip",exdir="./UCIrvine_DATA")
## Read table
library(data.table)
newFile <- fread("./UCIrvine_DATA/household_power_consumption.txt", na.strings = "?")
newFile <- newFile[,DateTime := as.POSIXct(paste(Date, Time), format = "%d/%m/%Y %H:%M:%S")]
## loading dplyr & subsetting data
library(dplyr)
library(lubridate)
subset_data <- filter(newFile, DateTime >= as.Date("2007-02-01 00:00:00"), DateTime < as.Date("2007-02-03 00:00:00"))
subset_data <- arrange(subset_data,DateTime)
rm(newFile)
png(filename="plot3.png",width=480,height=480)
with(subset_data,plot(Sub_metering_1,type="l",lwd=1, col="black",xlab="",ylab="Energy sub metering",axes=FALSE))
with(subset_data,points(Sub_metering_2,type="l",lwd=1,col="red", xlab=""))
with(subset_data,points(Sub_metering_3,type="l",lwd=1,col="blue", xlab=""))
axis(side=2, at=seq(0, 50, by=10))
axis(side=1, at=seq(0,nrow(subset_data),by=nrow(subset_data)/2), labels=c("Thu","Fri","Sat"))
legend("topright", legend=c("Sub_metering_1", "Sub_metering_2","Sub_metering_3"),
col=c("black","red", "blue"), lty=1,lwd=2)
box()
dev.off()
##cleanup
rm(subset_data)