forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
37 lines (28 loc) · 1.58 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
# Generate a line graph for Date/Time vs Energy sub metering and store in plot3.png
# Read 'household_power_consumption.txt' file
data<-read.table("household_power_consumption.txt", sep=";", header=TRUE, stringsAsFactors=FALSE, nrows=1000000,
na.strings = "?")
# Convert to Date to Date type
data$Date<- as.Date(data$Date,format="%d/%m/%Y")
# Subset required data ie data from the dates 2007-02-01 and 2007-02-02
sub <- subset(data, data$Date==as.Date("2007-02-01","%Y-%m-%d")|data$Date==as.Date("2007-02-02","%Y-%m-%d"))
# Remove read data from memory as its no longer required
rm(data)
# Create a DateTime column which stores concantenated Date and Time
sub$DateTime <- strptime(paste(sub$Date,sub$Time), format="%Y-%m-%d %H:%M:%S")
# Open a PNG Graphics Device with width=480 px and height=480 px and file name plot3.png
png("plot3.png", width=480, height=480)
# Plot a line graph for Date/Time vs Energy sub metering for Sub_metering_1
plot(sub$DateTime, sub$Sub_metering_1,
type="l", # plot lines
xlab="", # X label
ylab="Energy sub metering") # Y label
lines(sub$DateTime, sub$Sub_metering_2, col="red") # line graph for Sub_metering_2 with red line color
lines(sub$DateTime, sub$Sub_metering_3, col="blue") # line graph for Sub_metering_3 with blue line color
# Legend at top right for all lines in graph
legend("topright",
lty=1,
col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
# Shutdown graphics device
dev.off()