forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
37 lines (28 loc) · 1.2 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
30
31
32
33
34
35
36
37
skip <- 66637
end <- 69517
filename <- "household_power_consumption.txt"
mycolnames <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3")
df <- read.table(filename, sep = ";", na.strings = "?", skip = skip, nrows = end-skip)
colnames(df) <- mycolnames
png(file = "plot4.png")
DateTime <- paste(df$Date, df$Time)
DateTime <- as.POSIXct(DateTime, format = "%d/%m/%Y %H:%M:%S")
df$DateTime <- DateTime
par(mfrow=c(2,2))
# Plot 1
plot(df$DateTime, df$Global_active_power, type="l",
xlab = "", ylab = "Global Active Power")
# Plot 2
plot(df$DateTime, df$Voltage, type="l",
xlab = "datetime", ylab = "Voltage")
# Plot 3
plot(df$DateTime, df$Sub_metering_1, type="l", col = "black",
xlab = "", ylab = "Energy sub metering")
points(df$DateTime, df$Sub_metering_2, type="l", col = "red")
points(df$DateTime, df$Sub_metering_3, type="l", col = "blue")
legend("topright", col = c("black","red","blue"), lty=c(1,1), bty = "n",
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
# Plot 4
plot(df$DateTime, df$Global_reactive_power, type="l",
xlab = "datetime", ylab = "Global_reactive_power")
dev.off()