-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
21 lines (18 loc) · 1.23 KB
/
plot3.R
File metadata and controls
21 lines (18 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#First download and unzip the file from the mentioned location
path <- getwd()
url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(url, file.path(path, "exdata%2Fdata%2Fhousehold_power_consumption"))
# Now we are reading data into R
files <- file('household_power_consumption.txt')
dataPower <- read.table(text = grep("^[1,2]/2/2007",readLines(files),value=TRUE), sep = ';',
col.names = c("Date", "Time", "Global_active_power", "Global_reactive_power", "Voltage", "Global_intensity", "Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), na.strings = '?')
head(dataPower)
#Now we are plotting our third graph
SettingTime <-strptime(paste(dataPower$Date, dataPower$Time, sep=" "),"%d/%m/%Y %H:%M:%S")
dataPower1 <- cbind(SettingTime, dataPower)
png(filename = 'plot3.png', width = 480, height = 480, units='px')
plot(SettingTime,dataPower1$Sub_metering_1, xlab = '', ylab = 'Energy sub metering', type = 'l')
lines(SettingTime,dataPower1$Sub_metering_2,col="red")
lines(SettingTime,dataPower1$Sub_metering_3,col="blue")
legend('topright', col = c('black', 'red', 'blue'), legend = c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), lwd = 2)
dev.off()