forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
21 lines (21 loc) · 1.35 KB
/
plot4.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
data <- read.table("househole_power_consumption.txt", sep = ";", header = TRUE, na.string = "?")
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
data$Time <- format(strptime(data$Time, format = "%H:%M:%S"), format = "%H:%M:%S")
mod_data <- data[data$Date == "2007-02-01" | data$Date == "2007-02-02", ]
mod_data <- na.omit(mod_data)
mod_data$DT <- strptime(paste(mod_data$Date, mod_data$Time), format = "%Y-%m-%d %H:%M:%S")
png(file = "plot4.png")
par(mfrow = c(2,2))
par(bg = "transparent")
with(mod_data, plot(DT, Global_active_power, type = "n",ylab = "Global Active Power", xlab = ""))
lines(mod_data$DT, mod_data$Global_active_power, type = "l")
with(mod_data, plot(DT, Voltage, type = "n",xlab = "datetime", ylab = "Voltage"))
lines(mod_data$DT, mod_data$Voltage, type = "l")
with(mod_data, plot(DT, Sub_metering_1, type = "n", ylab = "Energy sub metering",xlab = ""))
lines(mod_data$DT, mod_data$Sub_metering_1, type = "l" )
lines(mod_data$DT, mod_data$Sub_metering_2, type = "l", col = "red" )
lines(mod_data$DT, mod_data$Sub_metering_3, type = "l", col = "blue" )
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),lty=c(1,1,1), col = c("black","red","blue"), merge = FALSE, bty = "n")
with(mod_data, plot(DT, Global_reactive_power, type = "n", xlab = "datetime"))
lines(mod_data$DT, mod_data$Global_reactive_power, type = "l")
dev.off()