forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
34 lines (28 loc) · 1.41 KB
/
plot4.R
File metadata and controls
34 lines (28 loc) · 1.41 KB
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 required libraries
library(gsubfn)
library(proto)
library(RSQLite)
library(DBI)
library(tcltk)
library(sqldf)
## Create data frame by reading the input file and filtering out the content.
household <- read.csv.sql("household_power_consumption.txt", sql = "select * from file where Date IN ('1/2/2007','2/2/2007')", header = TRUE, sep = ";")
## Adding a new column Date.Time to the data frame
household$Date.Time <- as.POSIXct(paste(household$Date, household$Time, sep =" "), format="%d/%m/%Y %H:%M:%S")
## Open PNG device; create "plot2.png" in working directory
png(file = "plot4.png", width = 480, height = 480, units = "px")
par(mfrow = c(2, 2))
## Creating the plot
with(household, {
plot(Date.Time, Global_active_power,type ='l', xlab = "", ylab = "Global Active Power")
plot(Date.Time, Voltage,type ='l', xlab = "datetime", ylab = "Voltage")
plot(Date.Time, Sub_metering_1, type = "n", xlab = "", ylab = "Energy sub metering")
lines(Date.Time, Sub_metering_1,col = "black")
lines(Date.Time, Sub_metering_2,col = "red")
lines(Date.Time, Sub_metering_3,col = "blue")
legend("topright", lty = 1, bty = "n", col = c("black", "red", "blue"), legend = c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
plot(Date.Time, Global_reactive_power,type ='l', xlab = "datetime", ylab = "Global_reactive_power")
})
## Closing data frame
unlink(household)
dev.off() ## Close the PNG file device