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
94 lines (71 loc) · 2.55 KB
/
plot3.R
File metadata and controls
94 lines (71 loc) · 2.55 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
rm(list=ls())
Sys.setlocale("LC_TIME", locale="USA")
installPackages <- function(x)
{
new.packages <- x[!(x %in% installed.packages()[,"Package"])]
if(length(new.packages)){
install.packages(new.packages)
}
}
loadLibraries <- function(x){
lapply(x, library ,character.only = TRUE)
}
getRootDir <- function(){
OS <- Sys.info()['sysname']
if (grepl(OS, "Windows")){
return('F:')
}else if (grepl(OS, "Linux")){ # linux osx must have curl installed
return('/media/thekeisoes/STORE N GO2')
}else{
return('/Volumes/STORE N GO')
}
}
getWorkingDirectoryPath <- function(directory){
OS <- Sys.info()['sysname']
if (grepl(OS, "Windows")){
return(file.path(getRootDir(), gsub('/','\\\\',directory)))
}else{
return(file.path(getRootDir(), gsub('\\\\','/',directory)))
}
}
installPackages(c('sqldf'))
loadLibraries(c('sqldf'))
OS <- Sys.info()['sysname']
workingDirectory <- getWorkingDirectoryPath('Coursera\\Exploratory Data Analysis\\Project1')
setwd(workingDirectory)
url <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
# windows environment
# if (grepl(OS, "Windows")){
# download.file(url, file.path(workingDirectory, 'household_power_consumption.zip'))
# }else{ # linux osx must have curl installed
# download.file(url, file.path(workingDirectory, 'household_power_consumption.zip'), method = 'curl')
# }
#unzip(file.path(workingDirectory, 'household_power_consumption.zip'))
# retrieve data
#dataset<-read.csv('household_power_consumption.txt', sep=';' ,na.strings="NA", stringsAsFactors=FALSE)
dataset <- read.csv.sql('household_power_consumption.txt',
sql='select * from file where Date=="2/2/2007" OR Date=="1/2/2007"',
sep=';' , stringsAsFactors=FALSE)
dataset <- na.omit(dataset)
Date <- as.Date(strptime(dataset$Date, '%d/%m/%Y'))
Time <- as.Date(strptime(dataset$Time, '%X'))
DateTime <- strptime(paste(dataset$Date, dataset$Time, sep = " "), '%d/%m/%Y %H:%M:%S')
DateTime[1]
dataset$Date=Date
dataset$Time=Time
dataset$DateTime=DateTime
str(dataset)
png('plot3.png' , width = 500, height = 500,
units = "px")
plot(dataset$DateTime,
as.numeric(dataset$Sub_metering_1),
type="l",
xlab="",
ylab = 'Energy sub metering')
lines(dataset$DateTime, as.numeric(dataset$Sub_metering_2), type = 'l', col='red')
lines(dataset$DateTime, as.numeric(dataset$Sub_metering_3), type = 'l', col='blue')
legend("topright",
col = c("black", "red", "blue"),
c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
lwd = 1)
dev.off()