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
45 lines (32 loc) · 1.41 KB
/
plot3.R
File metadata and controls
45 lines (32 loc) · 1.41 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
# Load packages
library(data.table)
# Check the library requs
library_check <- sapply("data.table", require, character.only = TRUE
, quietly = TRUE)
# Working the directories
path <- getwd()
if(!file.exists("./data")){
dir.create("./data")
}
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
# Download and unzip the dataset
download.file(url, file.path(path, "./data/DataFiles.zip"))
unzip(zipfile = "./data/DataFiles.zip")
# Load the dataset
data <- data.table::fread(input = "./data/household_power_consumption.txt",
na.strings="?")
data[, Global_active_power := lapply(.SD, as.numeric), .SDcols = c("Global_active_power")]
data[, dateTime := as.POSIXct(paste(Date, Time), format = "%d/%m/%Y %H:%M:%S")]
# subset the data to the desired time interval of two days
data <- data[(dateTime >= "2007-02-01") & (dateTime < "2007-02-02")]
png("plot3.png", width = 480, height = 480)
# create the png plot plot3
plot(x = data[,dateTime], y = data[, Sub_metering_1], type = "l", xlab = "", ylab = "Energy sub metering")
lines(x = data[, dateTime], y = data[, Sub_metering_2], col = "red")
lines(x = data[, dateTime], y = data[, Sub_metering_3], col = "blue")
legend("topright",
col = c("black", "red", "blue"),
c("Sub_metering_1 ","Sub_metering_2 ", "Sub_metering_3 "),
lty=c(1,1),
lwd=c(1,1))
dev.off()