forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
41 lines (29 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
37
38
39
40
41
library(dplyr)
setwd("c:/Users/David/Documents/GitHub/ExData_Plotting1")
#Location of raw data
filename <- "c:/Users/David/Documents/R/household_power_consumption.txt"
# Specify colClasses for read.table
colclasses = c(rep("character",2), rep("numeric",7))
# Load the power consumption datafile
all_power <- read.table(filename, sep=";", colClasses = colclasses, header=TRUE, na.strings="?")
#Convert dates to Date format
all_power$Date <- as.Date(all_power$Date, "%d/%m/%Y")
#Filter to only Feb 1 2007 through Feb 2 2007
feb_power <- filter(all_power, Date >= "2007-02-01" & Date <= "2007-02-02")
#Convert times to POSIXlt format
feb_power <- mutate(feb_power, DateTime = paste(Date, Time, sep = " "))
feb_power$DateTime <- strptime(feb_power$DateTime, "%Y-%m-%d %H:%M:%S")
# Open png device
png(filename = "plot3.png", width = 480, height=480)
# Plot lines for sub-metering stations vs. Day/Time
with(feb_power, plot(DateTime, Sub_metering_1, type="l", xlab = "", ylab = "Energy sub metering"))
with(feb_power, lines(DateTime, Sub_metering_2, col="red"))
with(feb_power, lines(DateTime, Sub_metering_3, col="blue"))
# Add a legend
legend("topright", lty=c(1,1,1), col=c("black", "red", "blue"),
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
# Copy plot to png file
#dev.copy(png, file="plot2.png")
dev.off()
# Remove the large dataset from memory
rm("all_power")