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
38 lines (30 loc) · 1.31 KB
/
Copy pathplot2.R
File metadata and controls
38 lines (30 loc) · 1.31 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
29
30
31
32
33
34
35
36
37
38
# Creating a plot to match the provided Plot 2.
fileLocation <- "/home/cheryl/coursera/jh-data-science/working_directory/4-cp1/household_power_consumption.txt"
powerData <- read.csv(fileLocation, sep = ";", header = TRUE,
colClasses = c("character", "character", rep("numeric", 7)),
na.strings = c("?"))
# We only want to plot the data from 2007-02-01 and 2007-02-02
currentPowerData <- powerData[powerData$Date == "1/2/2007" |
powerData$Date == "2/2/2007", ]
library(datasets)
png(file = "plot2.png")
# add a column to the data for POSIXct datetimes
currentPowerData$datetime <- apply(currentPowerData, 1, function(row) {
as.POSIXct(
strptime(
paste(row["Date"], row["Time"]),
"%d/%m/%Y %H:%M:%S",
tz = "GMT"),
tz = "GMT")
})
with(currentPowerData,
plot(datetime, Global_active_power,
xaxt = "n", # remove default tick marks from x axis
xlab = "", # remove x axis title
ylab = "Global Active Power (kilowatts)",
type = "l"))
# GMT epoch times for Feb 1 2007, Feb 2 2007, and Feb 3 2007 (midnight)
labelNums <- c(1170288000, 1170374370, 1170460740)
# add day of week labels to x axis
axis(side = 1, at = labelNums, labels = c("Thu", "Fri", "Sat"))
dev.off()