Repository navigation
Expand file tree
/
Copy pathplot3.R
More file actions
36 lines (32 loc) · 1.6 KB
/
Copy pathplot3.R
File metadata and controls
36 lines (32 loc) · 1.6 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
data_url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
data_zip_name <- "exdata_data_household_power_consumption.zip"
data_file_name <- "household_power_consumption.txt"
# Download file if not exists.
if (!file.exists(data_zip_name)){
if (.Platform$OS.type == "windows") {
library(downloader);
downloader::download(data_url, data_zip_name);
} else {
#can also use downloader::download
download.file(data_url, data_zip_name, method="curl");
}
}
data_file <- unz(data_zip_name, data_file_name);
## I've enough ram. Sqldf doesn't work with unz and grep is not installed on all systems
## Don't want to unzip file
## Might've used colClass = NULL for the columns, that i don't need,
## but have no time for this
data <- read.csv2(data_file, header = TRUE, na.strings = "?", dec=".",
colClasses = c(rep("character", 2), rep("numeric", 7)));
data <- data[(data$Date == "1/2/2007")|(data$Date == "2/2/2007"),]
data$TimeStamp <- strptime(paste(data$Date, data$Time), format="%d/%m/%Y %H:%M:%S")
#This line needed to get english names of weekdays on plot
Sys.setlocale("LC_TIME", "C")
png(filename="plot3.png", width=480, height=480, bg = NA)
with(data, plot(TimeStamp, data$Sub_metering_1 , type = "l", main = "",
ylab = "Energy sub metering", xlab="", col = "black"))
lines(data$TimeStamp, data$Sub_metering_2, col = "red")
lines(data$TimeStamp, data$Sub_metering_3, col = "blue")
legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lty=c(1,1,1),
col=c("black","red","blue"))
dev.off()