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
84 lines (74 loc) · 2.14 KB
/
plot4.R
File metadata and controls
84 lines (74 loc) · 2.14 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
rawDataFileName <- "../household_power_consumption.txt"
# Read the data from the file.
# For now keep the date and time as a character strings.
d <- read.table(rawDataFileName,
header = TRUE,
sep = ";",
na.strings = "?",
colClasses = c("character",
"character",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric",
"numeric"
),
comment.char = ""
)
# Keep only the needed dates.
# The dates appear to be in the D/M/YYYY format,
# without leading zeros.
d <- subset(d, Date %in% c("1/2/2007", "2/2/2007"))
# Convert the Date and Time character strings to a timestamp.
d$Timestamp <- as.POSIXct(paste(d$Date, d$Time, sep = " "),
format = "%d/%m/%Y %H:%M:%S"
)
# Drop the Date and Time character strings.
d <- d[, ! names(d) %in% c("Date", "Time")]
# Open the plot file
png("plot4.png")
# Set up a 2 by 2 matrix for the plots
par(mfcol = c(2, 2))
# Draw the Global_active_power over time.
plot(d$Timestamp, d$Global_active_power,
type = "l",
xlab = "",
ylab = "Global Active Power"
)
# Draw the Sub_metering_[1-3] over time.
plot(d$Timestamp, d$Sub_metering_1,
type = "l",
col = "black",
xlab = "",
ylab = "Energy sub metering"
)
lines(d$Timestamp, d$Sub_metering_2,
type = "l",
col = "red"
)
lines(d$Timestamp, d$Sub_metering_3,
type = "l",
col = "blue"
)
legend("topright",
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black", "red", "blue"),
lty = 1,
bty = "n"
)
# Draw the Voltage over time.
plot(d$Timestamp, d$Voltage,
type = "l",
xlab = "datetime",
ylab = "Voltage"
)
# Draw the Global_reactive_power over time.
plot(d$Timestamp, d$Global_reactive_power,
type = "l",
xlab = "datetime",
ylab = "Global_reactive_power"
)
# Close the plot file.
dev.off()