forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
44 lines (30 loc) · 1.76 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
37
38
39
40
41
42
43
44
library(data.table)
library(sqldf)
## Set file path
sourceFile<-"./household_power_consumption.txt"
destFile<-"./plot3.png"
## use sql commands to read only part of teh file
mySql <- "SELECT * from file WHERE Date = '1/2/2007' OR Date = '2/2/2007'"
dataSet<-read.csv2.sql(sourceFile,mySql)
## convert data.frame to data.table
dataSet<-as.data.table(dataSet)
## convert Date and Time to single DateTime column of type POSIXct
dataSet[,DateTime:=as.POSIXct(paste0(dataSet$Date,dataSet$Time),format=format("%d/%m/%Y%H:%M:%S"))]
## Set locale to English for plotting
Sys.setlocale("LC_TIME", "en_US.utf8")
## Plot the diagram
with(dataSet, plot(dataSet$DateTime,dataSet$Sub_metering_1,type="l",col="black",ylab="Energy sub metering", xlab=" "))
with(subset(dataSet), lines(dataSet$DateTime,dataSet$Sub_metering_2,type="l",col="red"))
with(subset(dataSet), lines(dataSet$DateTime,dataSet$Sub_metering_3,type="l",col="blue"))
legend("topright", text.width =strwidth("Sub_metering_1"), lty= c(1), col = c("black", "red","blue"), legend =c("Sub_metering_1", "Sub_metering_2","Sub_metering_3"))
## Copy screen device to PNG file
if(file.exists(destFile)) file.remove(destFile)
png(destFile, width=480, height=480)
## re-plot to png device
with(dataSet, plot(dataSet$DateTime,dataSet$Sub_metering_1,type="l",col="black",ylab="Energy sub metering", xlab=" "))
with(subset(dataSet), lines(dataSet$DateTime,dataSet$Sub_metering_2,type="l",col="red"))
with(subset(dataSet), lines(dataSet$DateTime,dataSet$Sub_metering_3,type="l",col="blue"))
legend("topright", text.width =strwidth("Sub_metering_1"), lty= c(1), col = c("black", "red","blue"), legend =c("Sub_metering_1", "Sub_metering_2","Sub_metering_3"))
##dev.copy(png, file=destFile,width=480,height=480)
## Close the png screen device
dev.off()