forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
34 lines (34 loc) · 2.06 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
## Loading the dplyr library
library(dplyr)
## Loading the file
household <- read.csv("household_power_consumption.txt", header=TRUE, sep=";")
## Select only the dates we are interested in
householdSub <- filter(household, Date=="1/2/2007" | Date=="2/2/2007")
## Formatting the dates
householdSub <- mutate(householdSub, DateTime = as.POSIXct(paste(Date, Time), format = '%d/%m/%Y %H:%M:%S'))
## Converting the factors as numerics
householdSub <- mutate(householdSub, Global_active_power = as.numeric(as.character(householdSub$Global_active_power)))
householdSub <- mutate(householdSub, Sub_metering_1 = as.numeric(as.character(householdSub$Sub_metering_1)))
householdSub <- mutate(householdSub, Sub_metering_2 = as.numeric(as.character(householdSub$Sub_metering_2)))
householdSub <- mutate(householdSub, Sub_metering_3 = as.numeric(as.character(householdSub$Sub_metering_3)))
householdSub <- mutate(householdSub, Voltage = as.numeric(as.character(householdSub$Voltage)))
householdSub <- mutate(householdSub, Global_reactive_power = as.numeric(as.character(householdSub$Global_reactive_power)))
## Create the file to draw in
png(file = "plot4.png", width = 480, height = 480)
## Define 2 plots per row / 2 plots per col
par("mfcol"= c(2,2))
## Setting the background color as transparent
par("bg"= "transparent")
## Drawing the plot1
with(householdSub, plot(DateTime, Global_active_power, type="l", xlab="", ylab="Global Active Power (kilowatts)"))
## Drawing the plot2
with(householdSub, plot(DateTime, Sub_metering_1, type="l", xlab="", ylab="Energy sub metering", bg="transparent"))
with(householdSub, (lines(DateTime, Sub_metering_2, col="red")))
with(householdSub, (lines(DateTime, Sub_metering_3, col="blue")))
legend("topright", bty="n", lty=1, col=c("black","red","blue"), legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
## Drawing the plot3
with(householdSub, plot(DateTime, Voltage, type="l", xlab="datetime", bg="transparent"))
## Drawing the plot4
with(householdSub, plot(DateTime, Global_reactive_power, type="l", xlab="datetime", bg="transparent"))
## Closing the device
dev.off()