forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
21 lines (14 loc) · 1.07 KB
/
plot3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Code for reading data and creating PLOT 3 assuming data is already downloaded and unzipped in your working directory,
#else first download data from https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
#READING AND FILTERING DATA BASED ON DATE
data<-read.table("household_power_consumption.txt",header=TRUE, sep = ";",na.strings = "?",stringsAsFactors = FALSE,colClasses = c(rep("character",2), rep("numeric",7)))
data<-data[data$Date %in% c("1/2/2007","2/2/2007"),]
data$Date <- strptime(paste(data$Date,data$Time),"%d/%m/%Y %T")
#CREATION OF PLOT 3 IN A PNG FILE
png(filename = "plot3.png", width = 480,height = 480, units = "px", bg = "white")
plot(data$Date,data$Sub_metering_1, type = "n",ylab = "Energy sub metering",xlab = "")
points(data$Date,data$Sub_metering_1, col = "black",type = "l")
points(data$Date,data$Sub_metering_2, col = "red",type = "l")
points(data$Date,data$Sub_metering_3, col = "blue",type = "l")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),col = c("black","red","blue"),lty = 1)
dev.off()