forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
42 lines (36 loc) · 1.6 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
# File written by Matt Frei on 12/9/2015.
# Coursera Exploratory Data Analysis class
# Project 1 - Plot 3
setwd('C:/Users/matt/Dropbox/Documents/Coursera/exploratory data analysis/Project 1/ExData_Plotting1')
######################################## Load Data ########################################
sc <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
download.file(sc,'data.zip')
unzip('data.zip')
cols <- c('date','time','global_active_power','global_reactive_power','voltage','global_intensity','sub_metering_1','sub_metering_2','sub_metering_3')
pd <- read.table('household_power_consumption.txt',
header=FALSE,
sep=';',
na.strings='?',
skip=66637,
nrows=2880,
col.names = cols,
stringsAsFactors=FALSE)
# I got the skip and nrows parameters using Bash commands:
# grep -n '1/2/2007' household_power_consumption.txt | head -n 1
# grep -n '^2/2/2007' household_power_consumption.txt | tail -n 1
pd$dt <- strptime(paste(pd$date,' ',pd$time),format=' %d/%m/%Y%H:%M:%S')
######################################## Begin plot 3 ########################################
png('plot3.png',width=480,height=480)
plot(pd$dt,pd$sub_metering_1,
ylab = 'Energy sub metering',
xlab='', type='l')
lines(pd$dt,pd$sub_metering_2,
col='red')
lines(pd$dt,pd$sub_metering_3,
col='blue')
legend('topright',
legend=c('sub_metering_1','sub_metering_2','sub_metering_3'),
col=c('black','red','blue'),
lty=c(1,1,1),
lwd=c(2,2,2))
dev.off()