forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
105 lines (90 loc) · 3.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## Exploratory Data Analysis
## Course project: Plotting4
plot4 <- function() {
# name of the plot
plotName <- "plot4.png"
# names of the data and zipped file
fileNameZip <- "exdata-data-household_power_consumption.zip"
fileNameTxt <- "household_power_consumption.txt"
dateFrom <- as.POSIXct("2007-02-01")
dateTo <- as.POSIXct("2007-02-02")
# check if data file exists. (in zip or txt format)
if (!file.exists(fileNameTxt)) {
if (!file.exists(fileNameZip)) {
stop(paste("No data file present! (",fileNameZip,"or",fileNameTxt,")"))
}
unzip(fileNameZip, fileNameTxt)
}
# read the data from the text data file
# first read the first row to get the first date in the data file
exdata <- read.table(fileNameTxt,
sep = ";",
header = TRUE,
na.strings = "?",
nrows = 1,
row.names = NULL,
colClasses = c("character","character","numeric","numeric","numeric",
"numeric","numeric","numeric","numeric"))
# get the columnnames
exdataColNames <- colnames(exdata)
# convert date and time column to one POSIXct Date column
exdata$Date <- as.POSIXct(strptime(paste(exdata$Date, exdata$Time), "%d/%m/%Y %H:%M:%S"))
# calculate the number of rows to skip and to get from the data file
dateTo <- dateTo + 24*60*60
dataSkip <- as.integer(difftime(dateFrom, exdata$Date[1], units = "mins")) + 1
dataRows <- as.integer(difftime(dateTo, dateFrom, units = "mins"))
# read the data from dateFrom to dateTo
exdata <- read.table(fileNameTxt,
sep = ";",
header = FALSE,
na.strings = "?",
skip = dataSkip,
nrows = dataRows,
col.names = exdataColNames,
row.names = NULL,
colClasses = c("character","character","numeric","numeric","numeric",
"numeric","numeric","numeric","numeric"))
# convert date and time column to one POSIXct Date column
exdata$Date <- as.POSIXct(strptime(paste(exdata$Date, exdata$Time), "%d/%m/%Y %H:%M:%S"))
exdata$Time <- NULL
# make plot
png(filename = plotName, width = 480, height = 480)
par(mfcol = c(2, 2), mar = c(4, 4, 2, 1), oma = c(0, 0, 0, 0))
# left upper graphic
plot(exdata$Date,
exdata$Global_active_power,
type = "l",
xlab = "",
ylab = "Global active power (kilowatts)" )
# left below graphic
plot(exdata$Date,
exdata$Sub_metering_1,
type = "l",
xlab = "",
ylab = "Energy sub metering" )
lines(exdata$Date,
exdata$Sub_metering_2,
col = "red")
lines(exdata$Date,
exdata$Sub_metering_3,
col = "blue")
legend("topright",
lty = c(1,1,1),
col = c("black","red", "blue"),
bty = "n",
legend = exdataColNames[c(7:9)])
# right upper graphic
plot(exdata$Date,
exdata$Voltage,
type = "l",
xlab = "",
ylab = "Voltage" )
# right below graphic
plot(exdata$Date,
exdata$Global_reactive_power,
type = "l",
xlab = "",
ylab = "Global reactive power" )
dev.off()
message(paste("Plotted", plotName))
}