forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot4.R
More file actions
27 lines (26 loc) · 1.19 KB
/
Plot4.R
File metadata and controls
27 lines (26 loc) · 1.19 KB
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
require(lubridate)
#read the data in
household<- read.table("household_power_consumption.txt", header=TRUE, sep= ";",na.strings="?",stringsAsFactors= FALSE)
#clean up the table
datetime<- within(household, datetime <- paste(Date,Time, sep=' '))
household<- datetime[,-c(1:2)]
household<- household[,c(8,1:7)]
#Convert the date and time
household$datetime <- dmy_hms(household$datetime)
#Subset
household<- subset(household, datetime> ymd("2007-02-01") & datetime < ymd("2007-02-03"))
#Drawing
png(file = "plot4.png")
par(mfrow= c(2,2))
#Graph1
with(household,plot(datetime,Global_active_power,type="l",xlab="",ylab="Global Active Power(kilowatts)", col="black"))
#Graph2
with(household,plot(datetime,Voltage,type="l",xlab="datetime",ylab="Voltage",lwd=2, col="black"))
#Graph3
with(household,plot(x=datetime,y=Sub_metering_1,xlab="",ylab="Energy Sub Metering",type="l",col="black"))
with(household,lines(x=datetime,y=Sub_metering_2,col="red"))
with(household,lines(x=datetime,y=Sub_metering_3,col="blue"))
legend("topright",col= c("black","red","blue"), legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),lwd=2,bty="n")
#Graph4
with(household,plot(datetime,Global_reactive_power,type="l",lwd=1))
dev.off()