forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plot3.R
44 lines (36 loc) · 1.7 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
41
42
43
44
# clear workspace
rm(list=ls())
# set working dir to ExData_Plotting1 dir
########################################
# set file name
filename = "exdata_plot1.zip"
# download file and save it to wd
if (!file.exists(filename)) {
retval = download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip",
destfile = filename,
)
}
# unzip file and read csv into dataframe
powerData = read.csv(unz(filename, "household_power_consumption.txt"), header=T,
sep=";", stringsAsFactors=F, na.strings="?",
colClasses=c("character", "character", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric"))
# set datetime format because of plo
powerData$timestamp = strptime(paste(powerData$Date, powerData$Time),
format="%d/%m/%Y %H:%M:%S", tz="UTC")
# start datetime
startDate = strptime("01/02/2007 00:00:00", format="%d/%m/%Y %H:%M:%S", tz="UTC")
# end datetime
endDate = strptime("02/02/2007 23:59:59", format="%d/%m/%Y %H:%M:%S", tz="UTC")
# filer data with given datetimes
powerData = powerData[powerData$timestamp >= startDate & powerData$timestamp <= endDate, ]
## Creating the plot
png(filename="plot3.png", width=480, height=480)
plot(powerData$timestamp, powerData$Sub_metering_1, type="l", xlab="",
ylab="Energy sub metering")
lines(powerData$timestamp, powerData$Sub_metering_2, col="red")
lines(powerData$timestamp, powerData$Sub_metering_3, col="blue")
legend("topright", legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col=c("black", "red", "blue"), lwd=par("lwd"))
dev.off()