forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
23 lines (18 loc) · 1.06 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
# Histogram for Global Active Power
setwd("C:/Sudhakar/coursera/04_ExploratoryDataAnalysis")
# read the data form the .txt file
data <- read.table("./data/household_power_consumption.txt",header = T, sep = ";",as.is=T,na.strings = "?")
head(data)
# convert and assing the formating for data field
data$Date <- as.Date(data$Date,"%d/%m/%Y")
# filter the date value based on the given dates
febdata <- data[data$Date == '2007-02-01' | data$Date == '2007-02-02',]
# loop through the 9 variables (9 columns)
for(i in 3:9) febdata[,i] <- as.numeric(febdata[,i])
# Generate the graph
png("./ExData_Plotting1/plot3.png")
with(febdata,plot(strptime(paste(Date,Time),"%Y-%m-%d %H:%M:%S"),Sub_metering_1,type='l',xlab="",ylab = 'Energy sub metering'))
with(febdata,lines(strptime(paste(Date,Time),"%Y-%m-%d %H:%M:%S"),Sub_metering_2,col='red'))
with(febdata,lines(strptime(paste(Date,Time),"%Y-%m-%d %H:%M:%S"),Sub_metering_3,col='blue'))
legend("topright",legend = c('Sub_metering_1','Sub_metering_2','Sub_metering_3'),col=c('black','red','blue'),lwd=1)
dev.off()