forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
29 lines (18 loc) · 1.09 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
26
plot4 <- function(){
# read the data file, then get the requried days
data <- read.table("household_power_consumption.txt", sep = ";", header = TRUE, na.strings = "?")
days <- data[which(data$Date == "1/2/2007" | data$Date == "2/2/2007"),]
days[,"DateTime"] <- as.POSIXct(paste(days$Date, days$Time), format="%d/%m/%Y %H:%M:%S")
# set up 2 x 2 grid in the device
png("plot4.png")
par(mfrow=c(2,2))
with(days, plot(DateTime, Global_active_power, type="l", ylab="Global Active Power", xlab=""))
with(days, plot(DateTime, Voltage, type="l", ylab="Voltage", xlab="datetime"))
with(days, plot(DateTime, Sub_metering_1, type="n", ylab="Energy sub metering", xlab=""))
points(days$DateTime, days$Sub_metering_1, type="l")
points(days$DateTime, days$Sub_metering_2, type="l", col="red")
points(days$DateTime, days$Sub_metering_3, type="l", col="blue")
legend(legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), col = c("black", "red", "blue"),lty=1, x = "topright")
with(days, plot(DateTime, Global_reactive_power, type="l", xlab="datetime"))
dev.off()
}