forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot3.R
36 lines (26 loc) · 1.25 KB
/
Plot3.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
packages_list <- c("dplyr")
new_packages <- packages_list[!(packages_list %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
library(dplyr)
# Read data in. Date and Time as character vectors. Will convert them in a later step
data <- read.csv(file = "household_power_consumption.txt",
header = TRUE,
sep = ";",
na.strings = "?",
colClasses = c(rep("character", 2), rep("numeric",5)))
# Date in format dd/mm/yyyy
# Only interested in 2007-02-01 and 2007-02-02
feb1and2 <- subset(data, Date == "1/2/2007" | Date == "2/2/2007")
feb1and2$DateTime <-
do.call(paste, c(feb1and2[c("Date", "Time")], sep = " ")) %>%
strptime(format = "%d/%m/%Y %H:%M:%S")
feb1and2$DayOfWeek <- weekdays(feb1and2$DateTime)
png(filename = "plot3.png", width = 480, height = 480)
plot(feb1and2$DateTime, feb1and2$Sub_metering_1, type = "l", main = "", xlab = "", ylab = "Energy Sub Metering")
lines(feb1and2$DateTime, feb1and2$Sub_metering_2, col = "red")
lines(feb1and2$DateTime, feb1and2$Sub_metering_3, col = "blue")
legend("topright",
c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col = c("black", "red", "blue"),
lty = c(1, 1, 1))
dev.off()