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
42 lines (33 loc) · 1.5 KB
/
plot3.R
File metadata and controls
42 lines (33 loc) · 1.5 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
39
40
41
42
# Plot 3
library(dplyr)
zipfile <- "exdata_household_power_consumption.zip"
textfile <- "household_power_consumption.txt"
# Check if we have already downloaded the file. If not, lets grab it.
if(!file.exists(zipfile)) {
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
,zipfile)
}
# Unzip the file and read in the data
unzip(zipfile, textfile)
householddata <- read.csv(textfile, na.strings = "?", sep=";",
stringsAsFactors = FALSE,
colClasses = c(rep("character",2),rep("numeric",7)))
# Change the Date to
householddata <- mutate(householddata, Date = as.Date(Date,"%d/%m/%Y"))
# Filter
householddata <- filter(householddata, Date == "2007-02-01" | Date == "2007-02-02")
# Amalgamage Date and Time
householddata <- mutate(householddata,
DateTime = as.POSIXct(paste(Date, Time),
format="%Y-%m-%d %H:%M:%S"))
png(filename = "plot3.png",
width = 480, height = 480, units = "px")
plot(householddata$DateTime,householddata$Sub_metering_1,
ylab="Energy sub metering", xlab = "",
col="black", type = "l")
points(householddata$DateTime,householddata$Sub_metering_2,
col="red", type="l")
points(householddata$DateTime,householddata$Sub_metering_3,
col="blue", type="l")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), col=c("black", "red","blue"),lty=1)
dev.off()