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
46 lines (36 loc) · 2.16 KB
/
plot3.r
File metadata and controls
46 lines (36 loc) · 2.16 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
#source("C:/Users/TB/Documents/COURSERA DATA SCIENCE test/COURSE4WK1/plot3.r")
#This function creates plot3.png in a local specified directory
plot3<-function() {
library(sqldf)
#We will only be using data from the dates 2007-02-01 and 2007-02-02
data<-read.csv.sql("C:/Users/TB/Documents/COURSERA DATA SCIENCE test/COURSE4WK1/household_power_consumption.txt", sql = "select * from file where V1 = '1/2/2007' ", header=FALSE, sep=";")
data1<-read.csv.sql("C:/Users/TB/Documents/COURSERA DATA SCIENCE test/COURSE4WK1/household_power_consumption.txt", sql = "select * from file where V1 = '2/2/2007' ", header=FALSE, sep=";")
con<-file("C:/Users/TB/Documents/COURSERA DATA SCIENCE test/COURSE4WK1/household_power_consumption.txt")
close(con)
data<-rbind(data, data1)
#Need to concat Date and Time columns together to acquire POSIXct class
dt<-paste(data$V1, data$V2)
t<-strptime(dt, format="%d/%m/%Y %T", tz="UTC")
data<-cbind(data,t)
colnam<-c('Date','Time','Global_active_power','Global_reactive_power','Voltage','Global_intensity','Sub_metering_1','Sub_metering_2','Sub_metering_3', "datetime")
colnames(data)<-colnam
#PLOT 3 - 480X480 as PNG
par(mfrow=c(1,1))
plot(data$datetime, data$Sub_metering_1, type='l', ylab="Energy sub metering", xlab="", col="black")
lines(data$datetime, data$Sub_metering_2, col="red")
lines(data$datetime, data$Sub_metering_3, col="blue")
#Adjusting legend border parameters so the output doesn't get cut off
leg <- legend("topright", lty = 1,
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"),
plot = FALSE)
leftlegendx <- (leg$rect$left - 10000)
rightlegendx <- (leftlegendx + 90000)
toplegendy <- leg$rect$top
bottomlegendy <- (leg$rect$top - leg$rect$h)
legend(x = c(leftlegendx, rightlegendx), y = c(toplegendy, bottomlegendy), lty = 1,
legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),
col = c("black","red","blue"))
dev.copy(png, file="C:/Users/TB/Documents/COURSERA DATA SCIENCE test/COURSE4WK1/PLOT3.png")
dev.off()
}