forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
29 lines (22 loc) · 1.24 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
27
28
29
start <- 66638
end <- 69517
nrows <- end - start
output <- "plot4.png"
data <- read.csv('household_power_consumption.txt', skip = 66638,
nrows = nrows, na.strings = "?", sep = ";")
colnames(data) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3")
data$DateTime <- strptime(paste(as.Date(data$Date, "%d/%m/%Y"), data$Time), "%Y-%m-%d %H:%M:%S")
png(file = output)
par(mfrow = c(2,2))
# graph (1,1) - Global Active Power
with(data, plot(DateTime, Global_active_power, type="l", xlab = "", ylab = "Global Active Power"))
# graph (1,2) - Voltage
with(data, plot(DateTime, Voltage, type="l", xlab = "datetime", ylab = "Voltage"))
# graph (2,1) - Energy Sub Metering
with(data, plot(DateTime, Sub_metering_1, type="l", xlab = "", ylab = "Energy sub metering", col="black"))
with(data, lines(DateTime, Sub_metering_2, col = "red"))
with(data, lines(DateTime, Sub_metering_3, col = "blue"))
legend("topright", legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lty=1, bty = "n")
# graph(2,2) - Global_reactive_power
with(data, plot(DateTime, Global_reactive_power, type="l", xlab = "datetime"))
dev.off()