Skip to content

Commit 908a3f1

Browse files
committed
plot 3
Figure and code to make plot 3
1 parent b6be545 commit 908a3f1

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

plot3.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
##Read in file
2+
temp<-read.table("household_power_consumption.txt", sep=";", header=T,
3+
stringsAsFactors=F)
4+
5+
##Subset data
6+
data<-subset(temp, temp$Date=="1/2/2007" | temp$Date=="2/2/2007")
7+
8+
## Create a variable of date and time and convert to POSIXct
9+
datetime<-paste(data$Date, data$Time)
10+
datetime<-as.POSIXct(datetime, format="%d/%m/%Y %H:%M:%S")
11+
12+
## Make the line plot of Energy sub metering as a png
13+
14+
nrg1<-as.numeric(data$Sub_metering_1)
15+
nrg2<-as.numeric(data$Sub_metering_2)
16+
nrg3<-as.numeric(data$Sub_metering_3)
17+
nrg<-data.frame(nrg1,nrg2,nrg3)
18+
datetime.df<-data.frame(datetime,datetime,datetime)
19+
png(file="pngplot3.png")
20+
plot(datetime.df[,1],nrg[,1],type="n",xlab="", ylab="Energy sub metering")
21+
points(datetime.df[,1],nrg[,1],type='l')
22+
points(datetime.df[,1],nrg[,2],type='l',col="red")
23+
points(datetime.df[,1],nrg[,3],type='l',col="blue")
24+
legend("topright", pch="-",col=c("black","blue","red"),
25+
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
26+
dev.off()

pngplot3.png

3.9 KB
Loading

0 commit comments

Comments
 (0)