forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
17 lines (17 loc) · 1.13 KB
/
plot3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
hpc<-read.csv("household_power_consumption.txt",sep=";",na.strings="?",colClasses=c("character","character","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
# Reading the file's content
hpc2<-hpc[hpc$Date=='1/2/2007' | hpc$Date=='2/2/2007',]
# Subsetting the file to get the two relevant days
hpc2$timestamp<-strptime(paste(hpc2$Date,hpc2$Time,sep=" "),format="%d/%m/%Y %H:%M:%S", tz="PST8PDT")
# Creating a timestamp from the date and time columns. We set the timezone to PST, but it is meaningless for the charting.
png(filename="plots/plot3.png",width=480,height=480)
# Creating the PNG with the size requested in the requirements
plot(x=hpc2$timestamp,y=hpc2$Sub_metering_1,type="l",xlab="",ylab="Energy Sub Metering")
# Generating the chart with metering 1 line
lines(x=hpc2$timestamp,y=hpc2$Sub_metering_2,col="red")
# Adding a red line for the metering 2 reading
lines(x=hpc2$timestamp,y=hpc2$Sub_metering_3,col="blue")
# Adding a blue line for the metering 3 reading
legend("topright",legend=paste("Sub_Metering_",1:3,sep=""),col=c("black","red","blue"),lty=1,lwd=3)
# Adding 3 legends for the 3 submetering types.
dev.off()