forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
25 lines (19 loc) · 1.29 KB
/
plot4.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
Sys.setlocale(locale = "C")
##Read Data
data <- read.table("household_power_consumption.txt",header = TRUE, sep = ";", na.strings='?',colClasses = c("character","character","numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"))
##Filter only 2007-02-01 and 2007-02-02
data<-data[as.Date(data$Date, format="%d/%m/%Y")>=as.Date('01/02/2007', format="%d/%m/%Y") & as.Date(data$Date, format="%d/%m/%Y")<=as.Date('02/02/2007', format="%d/%m/%Y"),]
#Create DateTime
data$DateTime<-paste(data$Date,data$Time)
data$DateTime<-strptime(data$DateTime, format="%d/%m/%Y %H:%M:%S")
#Plot the Graph
png(file = "plot4.png")
par(mfrow = c(2, 2))
plot(data$DateTime,data$Global_active_power,ylim=c(0,6),ylab='Global Active Power (kilowatts)',xlab='',type="l")
plot(data$DateTime,data$Voltage,ylab='Voltage',xlab='datetime',type="l")
plot(data$DateTime,data$Sub_metering_1,type="l",ylab='Energy Sub Metering',xlab='')
lines(data$DateTime,data$Sub_metering_2,type="l",col='red',lwd=2.5)
lines(data$DateTime,data$Sub_metering_3,type="l",col='blue',lwd=2.5)
legend('topright', legend=c('Sub_metering_1','Sub_metering_2','Sub_metering_3'),col=c('black','red','blue'),lty=c(1,1,1),lwd=c(2.5,2.5,2.5))
plot(data$DateTime,data$Global_reactive_power,ylab='Global_reactive_power',xlab='datetime',type="l")
dev.off()