forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
executable file
·36 lines (24 loc) · 1.56 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
library(sqldf)
setStartDate = strptime("16/12/2006;17:24:00", format = "%d/%m/%Y;%T")
subsetStartDate = strptime("2007-02-01;00:00:00", format = "%Y-%m-%d;%T")
subsetEndDate = strptime("2007-02-03;00:00:00", format = "%Y-%m-%d;%T") # +1 day to get corrent number of minutes
toSkip = difftime(subsetStartDate, setStartDate, units = "mins") + 1 # +1 to skip the header
toRead = difftime(subsetEndDate, subsetStartDate, units = "mins")
df = read.csv("household_power_consumption.txt", header = FALSE, sep = ";", skip = toSkip, nrows = toRead, na.strings = '?')
names(df) = c('Date', 'Time', 'Global_active_power', 'Global_reactive_power', 'Voltage', 'Global_intensity', 'Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3')
df["DateTime"] = NA
df$DateTime = paste(df$Date, df$Time, sep=";")
df = df[,c(10,3:9)]
df$DateTime = strptime(df$DateTime, format = "%d/%m/%Y;%T")
png(file = "plot4.png", width = 480, height = 480)
par(mfrow = c(2, 2))
with(df, {
plot(df$DateTime, df$Global_active_power, type='l', xlab = '', ylab = 'Global Active Power')
plot(df$DateTime, df$Voltage, type='l', xlab = 'datetime', ylab = 'Voltage')
plot(df$DateTime, df$Sub_metering_1, type='l', xlab = '', ylab = 'Energy sub metering')
lines(df$DateTime, df$Sub_metering_2, type='l', col='red')
lines(df$DateTime, df$Sub_metering_3, type='l', col='blue')
legend('topright', names(df)[6:8], lty = 1, col = c('black', 'red', 'blue'), bty = 'n')
plot(df$DateTime, df$Global_reactive_power, type='l', xlab = 'datetime', ylab = 'Global_reactive_power')
})
dev.off()