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
34 lines (30 loc) · 1.49 KB
/
Copy pathplot3.R
File metadata and controls
34 lines (30 loc) · 1.49 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
#Installing Packages
if(!require(tidyverse)){
install.packages("tidyverse")
}
library(tidyverse)
#Checking & Retrieving Data
filename <- "plottingdata.zip"
if(!file.exists(filename)){dir.create("filename")
filedata <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(filedata, filename, method = "curl")
}
#Unzipping Data
if (!file.exists("plottingdata")){dir.create("plottingdata")
plottingdata <- unzip(zipfile = filename, exdir = "plottingdata")
}
#Reading & Subsetting Data
powerconsumption <- read.table("plottingdata/household_power_consumption.txt", header = TRUE, sep = ";", stringsAsFactors = FALSE)
subdata <- powerconsumption %>% filter(Date == "1/2/2007" | Date == "2/2/2007")
subdata$Date <- as.Date(subdata$Date, format = "%d/%m/%Y")
subdata$sequential <- paste(subdata$Date, subdata$Time, sep = " ")
subdata$sequential <- strptime(subdata$sequential, format = "%Y-%m-%d %H:%M:%S")
#Third Plot
png(file = "plot3.png")
plot(subdata$sequential, subdata$Sub_metering_1, type = "n",
xlab = "", ylab = "Energy Sub Metering")
points(subdata$sequential, subdata$Sub_metering_1, type = "l", col = "red")
points(subdata$sequential, subdata$Sub_metering_2, type = "l", col = "blue")
points(subdata$sequential, subdata$Sub_metering_3, type = "l", col = "green")
legend("topright", col = c("red", "blue", "green"), lty = 1, legend = c("Sub Metering 1", "Sub Metering 2", "Sub Metering 3"))
dev.off()