forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
37 lines (34 loc) · 1.37 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
library(lubridate)
# the data file is expected to be in your working directory; you may set it here
#setwd("")
alldata <- read.table(file="household_power_consumption.txt",
header=TRUE,
sep=";",
dec=".",
fill=TRUE,
stringsAsFactors=FALSE,
na.strings="?")
# select only data from Febr 1st 2007 and Febr 2nd 2007
data <- subset(alldata, Date=="1/2/2007" | Date == "2/2/2007")
rm(alldata)
# combine the date and time parts
data$datetime <- dmy(data$Date) + hms(data$Time)
# we no longer need the Date and Time columns
data <- subset(data, select=-c(Date,Time))
# make sure the day of week labels appear in English
Sys.setlocale(category="LC_TIME", locale="en_US.UTF-8")
png(filename="plot3.png", width=480, height=480, units="px", bg="white")
# just draw the axes and labels and stuff – no lines yet
with(data, plot(datetime,
Sub_metering_1,
type="n",
xlab="",
ylab="Energy sub metering"))
with(data, lines(datetime, Sub_metering_1))
with(data, lines(datetime, Sub_metering_2, col="red"))
with(data, lines(datetime, Sub_metering_3, col="blue"))
legend("topright",
lty=1,
col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
dev.off()