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
38 lines (30 loc) · 1.4 KB
/
plot3.R
File metadata and controls
38 lines (30 loc) · 1.4 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
library(dplyr)
# Setting the working directory to the location of the data file.
setwd("d:/Github/Exploratory_Data/data")
#Read the file (Change Name if needed), delimited by semi colon.
d<- read.table("household_power_consumption.txt",header= TRUE, sep=";",stringsAsFactors=FALSE)
# Doing some exploring on the dataframe
names(d)
summary(d)
# Changing to a table to be used with dplyr
d<- tbl_df(d)
# Reduce the dataset to the dates of interest
d <- filter(d,Date == "1/2/2007" | Date == "2/2/2007")
# add a new column in Date format combining Date and Time
d$DateTime <- as.POSIXct(paste(d$Date, d$Time), format="%d/%m/%Y %H:%M:%S")
# Set the class to numeric on needed fields
d$Global_active_power<-as.numeric(d$Global_active_power)
d[,3]<-lapply(d[,3], as.numeric)
d[,4]<-lapply(d[,4], as.numeric)
d[,5]<-lapply(d[,5], as.numeric)
d[,6]<-lapply(d[,6], as.numeric)
d[,7]<-lapply(d[,7], as.numeric)
d[,8]<-lapply(d[,8], as.numeric)
# Linechart Plot 3
with(d,plot(DateTime,Sub_metering_1,type="l",ylab ="Energy sub metering",xlab=""))
with(d,lines(DateTime,Sub_metering_2,col="red"))
with(d,lines(DateTime,Sub_metering_3,col="blue"))
legend("topright",legend = c("Sub_metering_1 ","Sub_metering_2 ","Sub_metering_3 "),col=c("black","red","blue"),lty=1, text.font=1)
#Export as PNG.
dev.copy(png, file = "plot3.png", width=480, height=480)
dev.off()