forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
61 lines (50 loc) · 2.05 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
#Load the library and the file
library(dplyr)
dataset <- read.csv("household_power_consumption.txt", sep = ";")
#Filter-out the dates
min_date <- as.Date("01/02/2007",format = "%d/%m/%Y")
max_date <- as.Date("02/02/2007",format = "%d/%m/%Y")
dataset <- mutate(dataset,
DateFilter = as.Date(Date,format="%d/%m/%Y"))
dataset<-filter(dataset, DateFilter>=min_date & DateFilter<=max_date)
#Select Date and Time as Datetime and select
#the only relevant columns
dataset<-mutate(dataset,
Datetime =
as.POSIXct(strptime(paste(Date, Time), format="%d/%m/%Y %H:%M:%S"))) %>%
select(Datetime,Global_active_power:Voltage,Sub_metering_1:Sub_metering_3)
#Retrieve the names of the columns that are factors
columns <- names(dataset)[2:6]
#Convert the factors into numerics
for(column in columns){
numeric <- levels(dataset[,column])
dataset[,column]<-as.numeric(numeric[dataset[,column]])
}
#Setup the graphics device
png("plot4.png",width = 480,height = 480)
#Parametrize the group of charts
par(mfrow=c(2,2))
#Global Active Power - position (1,1)
with(dataset, plot(Datetime, Global_active_power,"n", xlab="",
ylab="Global Active Power (kilowatts)"))
with(dataset,lines(Datetime, Global_active_power))
#Voltage - position (1,2)
with(dataset, plot(Datetime, Voltage,"n", xlab="",
ylab="Voltage (V)"))
with(dataset,lines(Datetime, Voltage))
#Energy sub-metering (2,1)
with(dataset,
plot(Datetime,Sub_metering_1,"n", xlab = "", ylab="Energy sub metering"))
with(dataset,lines(Datetime,Sub_metering_1, col="black"))
with(dataset,lines(Datetime,Sub_metering_2, col="red"))
with(dataset,lines(Datetime,Sub_metering_3, col="blue"))
legend("topright",
legend = c("Sub_metering_1","Sub_metering_2", "Sub_metering_3"),
col = c("black","red","blue"),
lty = "solid",
bty = "n")
#Global Reactive Power
with(dataset, plot(Datetime, Global_reactive_power,"n", xlab="",
ylab="Global Reactive Power (kilowatts)"))
with(dataset,lines(Datetime, Global_reactive_power))
dev.off()