1+ # plot4.R to plot global active power multiplot
2+
3+ # # Read and subset data for proper dates
4+ data <- read.table(' household_power_consumption.txt' ,na.strings = " ?" ,header = T ,sep = " ;" ,colClasses = c(' character' ,' character' ,' numeric' ,
5+ ' numeric' , ' numeric' , ' numeric' ,' numeric' , ' numeric' , ' numeric' ))
6+ data $ DateTime <- strptime(paste(data $ Date , data $ Time )," %d/%m/%Y %H:%M:%S" )
7+ plotData <- subset(data ,as.Date(DateTime ) < = as.Date(" 2007-02-02" ) & as.Date(DateTime ) > = as.Date(" 2007-02-01" ))
8+
9+ # # Build plot and save png
10+ png(" plot4.png" ,height = 504 ,width = 504 ,bg = " transparent" )
11+ par(mfrow = c(2 ,2 ))
12+
13+ plot(plotData $ DateTime ,plotData $ Global_active_power ,pch = NA ,ylab = " Global Active Power" ,xlab = " " )
14+ lines(plotData $ DateTime ,plotData $ Global_active_power )
15+
16+ plot(plotData $ DateTime ,plotData $ Voltage ,xlab = " datetime" ,ylab = " Voltage" ,pch = NA )
17+ lines(plotData $ DateTime , plotData $ Voltage )
18+
19+ plot(plotData $ DateTime ,plotData $ Sub_metering_1 ,pch = NA ,ylab = " Energy sub metering" ,xlab = " " )
20+ lines(plotData $ DateTime ,plotData $ Sub_metering_1 ,col = " Black" )
21+ lines(plotData $ DateTime ,plotData $ Sub_metering_2 ,col = " Red" )
22+ lines(plotData $ DateTime ,plotData $ Sub_metering_3 ,col = " Blue" )
23+ legend(" topright" ,c(" Sub_metering_1" ," Sub_metering_2" ," Sub_metering_3" ),col = c(" Black" , " Red" , " blue" ),lty = c(1 ,1 ,1 ))
24+
25+ with(plotData ,plot(DateTime ,Global_reactive_power ,pch = NA ,xlab = " datetime" ))
26+ with(plotData ,lines(DateTime ,Global_reactive_power ))
27+
28+ dev.off()
0 commit comments