forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
38 lines (34 loc) · 1.08 KB
/
plot3.R
File metadata and controls
38 lines (34 loc) · 1.08 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
# Read the data
data <- read.table("household_power_consumption.txt",
header = TRUE,
sep = ";",
na.strings = "?",
colClasses = c(rep("character", 2), rep("numeric", 7)))
# Select the relevant days
data <- subset(data, Date == "1/2/2007" | Date == "2/2/2007")
# Replace the Date field with a datetime object
data$Date <- strptime(paste(data$Date, data$Time), "%d/%m/%Y %H:%M:%S")
# Open png device
png(filename = "plot3.png")
# Add the black points, labelling the axes
plot(data$Date, data$Sub_metering_1,
type = "l",
xlab = "",
ylab = "Energy sub metering")
# Add the red points to the plot
points(data$Date, data$Sub_metering_2,
type = "l",
col = "red")
# Add the blue points to the plot
points(data$Date, data$Sub_metering_3,
type = "l",
col = "blue")
# Add the legend
legend("topright",
legend = c("Sub_metering_1",
"Sub_metering_2",
"Sub_metering_3"),
col = c("black", "red", "blue"),
lty = 1)
# Close png device
dev.off()