forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
64 lines (56 loc) · 2.17 KB
/
plot4.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
62
63
64
#!/usr/bin/Rscript
#
# The script needs an internet connection to download data.
# Otherwise you have to provide either unzipped data file
# ('household_power_consumption.txt') or the downloaded
# ZIP archive (named 'data.zip') in the working directory.
#
# The output file (PNG image) is written to the working directory.
#data download and extraction (if necessary)
wd <- getwd()
filename <- 'household_power_consumption.txt'
csvfile <- file.path(wd, filename)
if (!file.exists(csvfile))
{
zipfile <- file.path(wd, 'data.zip')
if (!file.exists(zipfile))
{
url <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
download.file(url, zipfile, method="curl")
}
unzip(zipfile, c(filename), exdir = wd)
}
#data loading
data <- read.table(csvfile, header=T, sep=';', na.strings='?',
colClasses=c('factor', 'factor', 'numeric', 'numeric', 'numeric',
'numeric', 'numeric', 'numeric', 'numeric'))
datatransformed <- transform(data, datetime=strptime(paste(Date, Time), format='%e/%m/%Y %H:%M:%s'))
datatransformed$Date <- NULL
datatransformed$Time <- NULL
#filtering
start <- strptime('2007-02-01', format='%Y-%m-%d')
end <- strptime('2007-02-03', format='%Y-%m-%d')
dataofinterest <- subset(datatransformed, start <= datetime & datetime < end)
#plotting
library(graphics)
png(file=file.path(wd, 'plot4.png'), width=480, height=480, res=72,
bg='transparent')
par(mfcol=c(2,2))
with(dataofinterest,
{
plot(datetime, Global_active_power, type='n',
xlab='', ylab='Global Active Power (kilowatts)')
lines(datetime, Global_active_power, col='black')
plot(datetime, Sub_metering_1, type='n',
xlab='', ylab='Energy sub metering')
lines(datetime, Sub_metering_1, col='black')
lines(datetime, Sub_metering_2, col='red')
lines(datetime, Sub_metering_3, col='blue')
legend('topright', c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'),
col=c('black', 'red', 'blue'), lty=c(1,1,1))
plot(datetime, Voltage, type='n')
lines(datetime, Voltage, col='black')
plot(datetime, Global_reactive_power, type='n')
lines(datetime, Global_reactive_power, col='black')
})
dev.off()