forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
69 lines (59 loc) · 2.66 KB
/
plot2.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
## Exploratory Data Analysis
## Course project: Plotting2
plot2 <- function() {
# name of the plot
plotName <- "plot2.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)
plot(exdata$Date,
exdata$Global_active_power,
type = "l",
xlab = "",
ylab = "Global active power (kilowatts)" )
dev.off()
message(paste("Plotted", plotName))
}