forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.R
More file actions
28 lines (21 loc) · 1.16 KB
/
Copy pathplot2.R
File metadata and controls
28 lines (21 loc) · 1.16 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
## plot2()
##
## Purpose: Look at time-series data, plotting global active power for two days in February
##
## Note: Assumes the dataset has already been unzipped and is sitting in the current working directory
plot2 <- function() {
## Read in the data
data <- read.table("household_power_consumption.txt", header = T, sep = ";", colClasses = c(NA, NA, "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"), stringsAsFactors = F, na.strings = "?")
## Convert dates/times from character
data$Date <- as.Date(data$Date, format = "%d/%m/%Y")
data$Time <- strptime(data$Time, format = "%H:%M:%S")
## Restrict the dates to the first two days in February 2007
data <- data[(data$Date > "2007-01-31" & data$Date < "2007-02-03"), ]
## Generate our correct date-time variable
data$DateTime <- as.POSIXlt(paste(data$Date, format(data$Time, format = "%H:%M:%S")))
## Plot the Global Active Power data as a time series; generate PNG
png(filename = "plot2.png")
plot(data$DateTime, data$Global_active_power, type = "n", xlab = "", ylab = "Global Active Power (kilowatts)")
lines(data$DateTime, data$Global_active_power)
dev.off()
}