forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
36 lines (27 loc) · 1.1 KB
/
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
33
34
35
36
#use data.table library to speedup
library(data.table)
# before execution, first download and unzip the dataset,
# then set working directory to the folder of dataset
# output file name
output.file <- "plot2.png"
# read data file as a data.table
dt1 <- fread('household_power_consumption.txt',sep=";", header=T, na.string="?",
colClasses=rep("character",9))
# filter the required dates
dt2 <- dt1[Date == "1/2/2007" | Date == "2/2/2007"]
# convert to numeric
dt2[,Global_active_power := as.numeric(Global_active_power)]
# turn-off the local time format, avoid creating non-English output
Sys.setlocale("LC_TIME","C")
# concat Date & Time strings, then covert to date-time object
# in POSIXct format in a new column DateTime
dt2[, DateTime := as.POSIXct(paste(Date,Time),format="%d/%m/%Y %H:%M:%S")]
# open png file, with white background
png(output.file, width = 480, height = 480, units = "px",
bg='white')
# plot line
with(dt2, plot(x=DateTime,y=Global_active_power,type="l",
main="", xlab = "",
ylab="Global Active Power (kilowatts)"))
# close png file
dev.off()