This repository was archived by the owner on Oct 24, 2025. It is now read-only.
forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
45 lines (37 loc) · 1.4 KB
/
plot4.R
File metadata and controls
45 lines (37 loc) · 1.4 KB
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
38
39
40
41
42
43
44
45
library(data.table)
# load only data from rows starting with 1/2/2007 or 2/2/2007
cols <- colnames(fread("data/household_power_consumption.txt", nrows = 0))
data <- fread("grep ^[12]/2/2007 data/household_power_consumption.txt")
setnames(data, cols)
# combine the Date and Time columns into something useful
data[, DateTime := as.POSIXct(strptime(paste(Date, Time), "%d/%m/%Y %H:%M:%S"))]
# make plot 4
png("plot4.png", width = 480, height = 480)
par(mfrow = c(2, 2))
# top left sub-plot
with(data, {
plot(DateTime, Global_active_power, type = "n", xlab = "",
ylab = "Global Active Power")
lines(DateTime, Global_active_power, type = "l")
})
# top right sub-plot
with(data, {
plot(DateTime, Voltage, type = "n", xlab = "datetime", ylab = "Voltage")
lines(DateTime, Voltage, type = "l")
})
# bottom left sub-plot
cols <- c("black", "red", "blue")
with(data, {
plot(DateTime, Sub_metering_1, type = "n", xlab = "",
ylab = "Energy sub metering")
lines(DateTime, Sub_metering_1, type = "l", col = cols[1])
lines(DateTime, Sub_metering_2, type = "l", col = cols[2])
lines(DateTime, Sub_metering_3, type = "l", col = cols[3])
})
legend("topright", legend = names(data)[7:9], lty = 1, col = cols, bty = "n")
# bottom right sub-plot
with(data, {
plot(DateTime, Global_reactive_power, type = "n", xlab = "datetime")
lines(DateTime, Global_reactive_power, type = "l")
})
dev.off()