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
57 lines (53 loc) · 2.02 KB
/
plot3.R
File metadata and controls
57 lines (53 loc) · 2.02 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
53
54
55
56
57
## Function to get user confirmation
## Only quit with "y" or "n"
readconfirm <- function() {
n <- readline(prompt="Do you want to continue? (y/n): ")
if(n=="y" || n=="n") {
return(n)
} else {
return(readconfirm())
}
}
readData <- function() {
file <- "./data/household_power_consumption.txt"
library(data.table)
## Estimating table size
top.size <- object.size(fread(file, nrow=1000))
lines <- as.numeric(gsub("[^0-9]", "", system(paste("wc -l", file, sep = " "), intern=T)))
size.estimate <- lines / 1000 * top.size
## Asking user if he want to continue
print(paste("You will manipulate a table of size: ", size.estimate, " bytes"))
confirm <- readconfirm()
if (confirm=="n")
return()
## Loading data of days "1/2/2007", "2/2/2007"
findRows<-fread(file, header = TRUE, select = 1)
all<-(which(findRows$Date %in% c("1/2/2007", "2/2/2007")) )
skipLines<- min(all)
keepRows<- length(all)
data <- fread(file, skip = (skipLines) , nrows = keepRows, header = FALSE)
rm(findRows)
dataNames<- names(fread(file, nrow = 1))
setnames(data, dataNames)
return(data)
}
plot3 <- function(){
data <- readData()
if (!is.null(data)){
png("plot3.png")
Sys.setlocale("LC_TIME", "en_US.UTF-8")
plot(strptime(paste(data$Date, data$Time ,sep = " "), format = "%d/%m/%Y %H:%M:%S"),
data$Sub_metering_1, type = "l", ylab = "Energy sub metering",
xlab = "", col="black")
lines(strptime(paste(data$Date, data$Time ,sep = " "), format = "%d/%m/%Y %H:%M:%S"),
data$Sub_metering_2, col="red")
lines(strptime(paste(data$Date, data$Time ,sep = " "), format = "%d/%m/%Y %H:%M:%S"),
data$Sub_metering_3, col="blue")
legend("topright", lwd=2, col = c("black", "blue", "red"),
legend = c("Sub_metering_1","Sub_metering_2", "Sub_metering_3"))
dev.off()
print("plot3.pgn is done!")
} else {
print("See you!")
}
}