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
50 lines (38 loc) · 1.89 KB
/
Copy pathplot3.R
File metadata and controls
50 lines (38 loc) · 1.89 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
plot3 <- function() {
## Plot 3 for Exploritory Data Analysis Project 1
## Uses the Electric power consumption dataset from UCI Machine Learning
## Measurements of electric power consumption in one household with
## a one-minute sampling rate over a period of almost 4 years. Different
## electrical quantities and some sub-metering values are available.
library(sqldf)
## Create a directory for the download
dir.create("./data/", showWarnings = FALSE)
## Set data file variables
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
destination <- "./data/epc.zip"
## If the file has already been downloaded, skip the download
if (!file.exists(destination)) {
download.file(url, destination)
message("File downloaded")
}
message("File already downloaded")
## Use sql to read in only the data for the choosen dates
data <- read.csv.sql(unzip(destination),
sql = "select * from file where Date in ('1/2/2007','2/2/2007')",
header = TRUE, sep = ";")
## Combine Date and Time into a single column and change to Date class
data <- cbind(paste(data$Date, data$Time), data)
names(data) <- c("Datetime", names(data)[2:10])
data$Datetime <- strptime(data$Datetime, format = "%d/%m/%Y %H:%M:%S")
## Create a 480 x 480 PNG file with the requested plot
png(file = "./plot3.png", width = 480, height = 480) ## open PNG device
with(data, plot(Datetime, Sub_metering_1, type = "l",
ylab = "Energy sub metering",
xlab = ""))
with(data, lines(Datetime, Sub_metering_2, col="red"))
with(data, lines(Datetime, Sub_metering_3, col="blue"))
legend("topright", lty = 1, col = c("black", "red", "blue"),
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
dev.off() ## Don't forget to close the device
message("PNG file created")
}