forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
32 lines (22 loc) · 898 Bytes
/
plot2.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
library(data.table)
library(sqldf)
## Set file path
sourceFile<-"./household_power_consumption.txt"
destFile<-"./plot2.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
plot(dataSet$DateTime,dataSet$Global_active_power, type="l",xlab="", ylab="Global Active Power (kilowatts)"
)
## Copy screen device to PNG file
if(file.exists(destFile)) file.remove(destFile)
dev.copy(png, file=destFile,width=480,height=480)
## Close the screen device
dev.off()