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
39 lines (31 loc) · 1.47 KB
/
plot2.R
File metadata and controls
39 lines (31 loc) · 1.47 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
39
plot2 <- function() {
library(data.table)
library(lubridate)
library(dplyr)
### Check if the zip file exists, if it does not exist, then download file
if (!file.exists("exdata-data-household_power_consumption.zip")) {
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(url, destfile = "./exdata-data-household_power_consumption.zip")
}
##unzip the file, but remove the file before unzipping
if (file.exists("household_power_consumption.txt")){
file.remove("household_power_consumption.txt")
}
unzip("./exdata-data-household_power_consumption.zip")
#read the fulldata
cc <- c("Date")
fd <- read.table("household_power_consumption.txt", sep=";"
,na.strings="?", header = TRUE, stringsAsFactors=FALSE)
fd <- data.table(fd)
# get only the 1st and 2nd Feb 2007 data into another table
# then merge the Date and Time columns into a POSIXct column
# and set the column order
d <- fd[Date == "1/2/2007" | Date == "2/2/2007", ]
d$DateTime <- dmy_hms(paste(d$Date, d$Time))
mydt <- d %>% select(-Date, -Time) %>%
setcolorder(c(8,1:7))
# Now, we have data in the format we want and can go on to plotting
png(filename="plot2.png", width=480, height=480)
with(mydt, plot(DateTime, Global_active_power, type="l", xlab="", ylab="Global Active Power (kilowatts)"))
dev.off()
}