forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
61 lines (50 loc) · 1.56 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Clean environment
rm(list = ls())
objects()
search()
intersect(objects(), search())
# Libraries used
library(tidyverse)
library(lubridate)
# Download data
fileURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(url = fileURL, destfile = "Electric_power_consumption.zip", method = "curl")
# Unzip file
unzip(zipfile = "Electric_power_consumption.zip")
# Read data
powerCons <-
read.table(
file = "household_power_consumption.txt",
header = T,
sep = ";",
na.strings = "?",
stringsAsFactors = F
)
powerCons$Date <- dmy(powerCons$Date)
# Resize dataset since we work with the dates 2007-02-01 and 2007-02-02 only
powerCons <- powerCons %>%
filter(Date == "2007-02-01" | Date == "2007-02-02")
# Creating DateTime variable
powerCons$DateTime <- paste(powerCons$Date, powerCons$Time, sep = "_")
powerCons$DateTime <- ymd_hms(powerCons$DateTime)
# Open PNG graphics device
png(filename = "plot3.png", width = 480, height = 480, units = "px")
plot(
powerCons$DateTime,
powerCons$Sub_metering_1,
type = "l",
xlab = "",
ylab = "Energy sub metering"
)
lines(powerCons$DateTime,
powerCons$Sub_metering_2, col = "red")
lines(powerCons$DateTime,
powerCons$Sub_metering_3, col = "blue")
legend(
x = "topright",
legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
lty = 1,
col = c("black", "red", "blue")
)
# Close graphics device
dev.off()