forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
34 lines (28 loc) · 925 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
33
34
# Plot Global active power as a function of time
# and save to a PNG file with a
# width of 480 pixels and a height of 480 pixels.
#' Load the data into a table
#' @return d the data table
loadData <- function() {
# The data file
f<- 'household_power_consumption.txt'
# Load a subset of the data
q <- "SELECT * from file WHERE Date = '1/2/2007' OR Date = '2/2/2007'"
d<- read.csv.sql(f, sql=q, header=TRUE,
stringsAsFactors=FALSE, sep=";")
# Replace the '?' na
d[d=='?']=NA
# Convert date from character to Date format
d$Time <- as.POSIXct(strptime(paste(d$Date, d$Time), "%d/%m/%Y %H:%M:%S"))
d$Date <- as.Date(d$Date, "%d/%m/%Y")
d
}
# Read in the data
d<-loadData()
# Plot
png(filename = "plot2.png", width = 480, height = 480, bg = "white")
plot(d$Time, d$Global_active_power,
ylab="Global Active Power (kilowatts)",
xlab="",
type='l')
dev.off()