-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
61 lines (38 loc) · 2.21 KB
/
plot4.R
File metadata and controls
61 lines (38 loc) · 2.21 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
51
52
53
54
55
56
57
58
59
60
61
plot4 <- function(){
# Load useful packages
library(data.table)
library(dplyr)
# Download data
if(!file.exists('data.zip')){
url <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
download.file( url = url, destfile = 'data.zip', method = 'curl')
}
#Extract data
if(!file.exists('household_power_consumption.txt')){
unzip('data.zip')
}
# Put and clean data into R
data <- fread('household_power_consumption.txt', sep=';', na.strings = '?')
data <- filter(data, Date == '1/2/2007' | Date == '2/2/2007')
data <- as.data.frame(data)
data[ , 1] <- as.Date(data[ , 'Date'], '%d/%m/%Y' )
Date <- paste(data[ , 'Date'], data[ , 'Time'] )
Date <- as.POSIXct(Date, '%Y-%m-%d %H:%M:%S')
data <- select(data, -c(Date, Time) )
data <- cbind(Date, data)
# Create multiple plot
par(mfrow = c(2,2))
plot( Global_active_power ~ Date, data, type='l', xlab='', ylab = 'Global Active Power (kilowatts)' ) #Plot 1
plot( Voltage ~ Date, data, type='l', xlab='datetime', ylab = 'Voltage' ) #Plot 2
with(data, {
plot( Sub_metering_1 ~ Date, type='n', xlab='', ylab = 'Energy sub metering')
points( Sub_metering_1 ~ Date, type='l', xlab='', ylab = 'Energy sub metering', col = 'black' )
points( Sub_metering_2 ~ Date, type='l', xlab='', ylab = 'Energy sub metering', col = 'red' )
points( Sub_metering_3 ~ Date, type='l', xlab='', ylab = 'Energy sub metering', col = 'blue' )
legend('topright', pch= '-' , col = c('black', 'red', 'blue'), legend = c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'))
})
plot( Global_reactive_power ~ Date, data, type='l', xlab='datetime', ylab = 'Global_reactive_power' ) #Plot 4
# Save plot number 4
dev.copy(png, file ='plot4.png')
dev.off()
}