forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
41 lines (27 loc) · 1.53 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
33
34
35
36
37
38
39
library(dplyr)
library(lubridate)
download.file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", destfile = "./HPC.zip", method = "curl")
if(!file.exists("household_power_consumption")){unzip("./HPC.zip")}
data <- read.table("./household_power_consumption.txt",sep =";", header =TRUE)
head(data)
sapply(data, class)
data1<-mutate(data, NewDate = as.Date(data$Date, "%d/%m/%Y"))
data2<- filter(data1, NewDate=="2007-02-02"| NewDate =="2007-02-01")
data2$Global_active_power<- as.character(Global_active_power))
data2$Global_active_power<- as.numeric(Global_active_power))
data2$NewTime <-as.character(data2$Time)
data2$RealTime <- paste(data2$NewDate, data2$NewTime)
data2$RealTime <- ymd_hms(data2$RealTime)
data2$Sub_metering_1<-as.character(data2$Sub_metering_1)
> data2$Sub_metering_1<-as.numeric(data2$Sub_metering_1)
data2$Sub_metering_2<-as.character(data2$Sub_metering_2)
data2$Sub_metering_2<-as.numeric(data2$Sub_metering_2)
png(file ="plot3.png", width = 480, height = 480)
with(data2, plot(RealTime, Sub_metering_2, pch =NA_integer_))
with(data2, plot(RealTime, Sub_metering_3, pch =NA_integer_))
with(data2, plot(RealTime, Sub_metering_1, pch =NA_integer_, xlab ="", ylab = "Energy sub metering"))
with(data2, lines(RealTime, Sub_metering_1, col = "black"))
with(data2, lines(RealTime, Sub_metering_2, col = "red"))
with(data2, lines(RealTime, Sub_metering_3, col = "blue"))
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),col=c("black","red","blue"), lty = 1)
dev.off()