forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
34 lines (31 loc) · 1.89 KB
/
plot4.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
#Read the data
#download.file(url = "https://d396qusza40orc.cloudfront.net/exdata/data/household_power_consumption.zip" , destfile = "household_power_consumption.zip", method="curl")
#unzip("household_power_consumption.zip")
dados <- read.table("household_power_consumption.txt", header= TRUE, sep= ";", na.strings="?", colClasses = c("character", "character", "numeric", "numeric","numeric","numeric","numeric","numeric"))
dados2 <- dados[as.Date(dados$Date, format="%d/%m/%Y") >= "2007-02-01" & as.Date(dados$Date, format="%d/%m/%Y") <= "2007-02-02", ]
#Parse the data time characters
for (i in seq_along(dados2[,1])){
data <- dados2[i, "Date"]
hora <- dados2[i, "Time"]
data_hora <- paste(data, hora)
res <- strptime(data_hora, "%d/%m/%Y %H:%M:%S")
dados2[i, "date_time"] <- as.POSIXct(res)
}
png(filename = "plot4.png", width = 480, height = 480)
par(mfrow=c(2,2))
#Plot 1, 2
plot(Global_active_power ~ date_time, dados2, type="l", xaxt="n", ylab="Global Active Power", xlab="")
axis(1, at= quantile(dados2$date_time , probs=c(0, 0.5 , 1)), label=c("Thu", "Fri", "Sat") )
#Plot 1,2
plot(Voltage ~ date_time, dados2, type="l", xaxt="n", ylab="Voltage", xlab="datetime")
axis(1, at= quantile(dados2$date_time , probs=c(0, 0.5 , 1)), label=c("Thu", "Fri", "Sat") )
#plot 2,1
plot(Sub_metering_1 ~ date_time, dados2, type="l", xaxt="n", ylab="Energy submetering", xlab="", col="black")
lines(Sub_metering_2 ~ date_time, dados2, col="red")
lines(Sub_metering_3 ~ date_time, dados2, col="blue")
legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=c(1,1), col=c("black", "red","blue"), bty="n")
axis(1, at= quantile(dados2$date_time , probs=c(0, 0.5 , 1)), label=c("Thu", "Fri", "Sat") )
#plot 2,2
plot(Global_reactive_power ~ date_time, dados2, type="l", xaxt="n", xlab="datetime")
axis(1, at= quantile(dados2$date_time , probs=c(0, 0.5 , 1)), label=c("Thu", "Fri", "Sat") )
dev.off()