forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
41 lines (34 loc) · 1.14 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
# 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 = "plot3.png", width = 480, height = 480, bg = "white")
plot(d$Time, d$Sub_metering_1,
ylab="Energy sub metering",
xlab="",
type='l', col="black")
lines(d$Time, d$Sub_metering_2, col="red")
lines(d$Time, d$Sub_metering_3, col="blue")
legend("topright", border=legendBorderColour,
col=c("black", "red", "blue"),
c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
lty=1)
dev.off()