forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
70 lines (51 loc) · 2.28 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
library(lubridate)
library(dplyr)
library(readr)
## Defining the internet source for the download
source <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
## Downloading zipped files in Data folder
if (!file.exists("./Data")) {
dir.create("./Data")
}
download.file(url = source, destfile = "./Data/Exploratory_data_analysis.zip")
## Unzip the zipped files
unzip(zipfile = "./Data/Exploratory_data_analysis.zip",exdir = "./Data")
## Change the Working Directory and read the data file
pwrConsumption <- read.table("./Data/household_power_consumption.txt", sep = ";", header = TRUE)
## Format the fields
## Change the column to Data/ Time and Numeric format
x <- names(pwrConsumption)
for (column in x){
if (column == "Date"){
pwrConsumption[,column] <- dmy(pwrConsumption[,column])
}else if (column == "Time"){
pwrConsumption[,column] <- hms(pwrConsumption[,column])
}else{
pwrConsumption[,column] <- as.numeric(pwrConsumption[, column])
}
}
## Filter the data for required dates
pwr<- filter(pwrConsumption, Date == "2007-02-01" | Date == "2007-02-02")
## Add the Day and Date_long columns using dplyr
pwr <- pwr%>%
mutate(Day = weekdays(Date, abbreviate = TRUE))
pwr <- pwr%>%
mutate(Date_long = Date+Time)
## Setting plot Area
par(mfrow = c(2,2), mar = c(4,4,2,2))
## Plotting graphs and review them
## Plot1
with(pwr, plot(Date_long, Global_active_power, type = "l", xlab = "", ylab = "Global Active Power(kilowatts)"))
## Plot2
with(pwr, plot(Date_long, Voltage, type = "l", xlab = "datetime", ylab = "Voltage"))
## Plot3
with(pwr, plot(Date_long, Sub_metering_1, type = "l", col = "black", xlab = "", ylab = "Energy sub metering"))
with(pwr, points(Date_long, Sub_metering_2, type = "l", col = "red"))
with(pwr, points(Date_long, Sub_metering_3, type = "l", col = "blue"))
## Adding Legends to the plot
legend("topright", lty = c(1,1,1), lwd = c(1,1,1), col = c("black", "red", "blue"), legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
## Plot4
with(pwr, plot(Date_long, Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power"))
## Copy the plots to png file
dev.copy(png, "./plot4.png", width = 480, height = 480)
dev.off()