forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
34 lines (31 loc) · 1.09 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
library(tidyverse)
library(lubridate)
#Download data
if(!dir.exists('data')) {
dir.create('data')
url <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
temp <- tempfile()
download.file(url, temp)
unzip(temp, exdir = 'data')
unlink(temp)
}
data <- read_delim(
file.path('data','household_power_consumption.txt'),
';',
na = '?')
plotData <- data %>%
mutate(Date = dmy(Date), Time = hms(Time)) %>%
filter(Date %in% ymd(c(' 2007-02-01', ' 2007-02-02')))
png('plot3.png')
with(plotData, plot(Date+Time, Sub_metering_1,
type = 'n',
ylab = 'Enegy sub metering',
xlab = ''))
with(plotData, lines((Date + Time), Sub_metering_1, type = 'l', col = 'red'))
with(plotData, lines((Date + Time), Sub_metering_2, type = 'l', col = 'green'))
with(plotData, lines((Date + Time), Sub_metering_3, type = 'l', col = 'blue'))
legend('topright',
legend = c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'),
lty = c(1, 1, 1),
col = c('red', 'green', 'blue'))
dev.off()