forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
52 lines (39 loc) · 1.91 KB
/
Copy pathplot3.R
File metadata and controls
52 lines (39 loc) · 1.91 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
40
41
42
43
44
45
46
47
48
49
50
51
52
# Construct the plot and save it to a PNG file with a width of 480 pixels
# and a height of 480 pixels.
# Name each of the plot files as plot1.png, plot2.png, etc
# Create a separate R code file (plot1.R, plot2.R, etc.) that constructs the
# corresponding plot, i.e. code in plot1.r constructs the plot1.png plot. Your
# code file should include code for reading the data
# so that the plot can be fully reproduced. You must also include the code that
# creates the PNG file.
# Add the PNG file and R code file to the top-level folder of your git repository
# (no need for separate sub-folders)
# We will only be using data from the dates 2007-02-01 and 2007-02-02.
library(lubridate)
library(dplyr)
library(data.table)
# ordinarily this function would be in a separate file, sourced in and not
# duplicated at the top of every file but I include it so that every file is
# self contained for the purpose of this exercise
readFile <- function() {
file <- "household_power_consumption.txt"
range <- c(ymd("2007-02-01"), ymd("2007-02-03"))
data <- fread(file, sep=";", na.strings="?", stringsAsFactors = T, data.table = F, showProgress = F) %>%
mutate(DateTime = dmy_hms(paste(Date, Time))) %>%
filter(DateTime < range[2] & DateTime >= range[1]) %>%
select(c(10, 3:9))
# convert the numerical fields from factors to numbers
lapply(2:8, function(x) { data[, x] <<- as.numeric(as.character(data[, x])); NULL })
# return table
tbl_df (data)
}
createChart <- function(data) {
png(filename="plot3.png", width=480, height=480, bg="transparent")
plot(data$DateTime, data$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
lines(data$DateTime, data$Sub_metering_2, col="red")
lines(data$DateTime, data$Sub_metering_3, col="blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=1, col=c("black", "red", "blue"))
dev.off()
}
data <- readFile()
createChart(data)