forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
32 lines (25 loc) · 1.15 KB
/
plot3.R
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
#plot3.R
#get Power Consumption Data
file <- 'household_power_consumption.txt'
powerConsumptionData <- read.csv(file, sep=';', stringsAsFactors=F)
powerConsumptionData <- subset(powerConsumptionData,
powerConsumptionData$Date=='1/2/2007' | powerConsumptionData$Date=='2/2/2007')
#Convert time to single vector for date + time
powerConsumptionData$DateTime <- strptime(paste(powerConsumptionData$Date,powerConsumptionData$Time),
'%d/%m/%Y %T')
#set graphics device
png(file="plot3.png")
#Build Plot 3
with(powerConsumptionData,plot(DateTime,Sub_metering_1,
type='n',
ylab='Energy sub metering',
xlab=''))
#Add Lines to Plot 3
with(powerConsumptionData,lines(DateTime,Sub_metering_1))
with(powerConsumptionData,lines(DateTime,Sub_metering_2, col='red'))
with(powerConsumptionData,lines(DateTime,Sub_metering_3, col='blue'))
#Add Legend to Plot 3
legend('topright', lty=1, col=c('black','red','blue'),
legend=c('Sub_metering_1','Sub_metering_2','Sub_metering_3'))
#Shut down graphics device
dev.off()