forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
63 lines (48 loc) · 2.08 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
## Exploratory Analysis Course Project 1
## Filename: plot4.R
## Author: [email protected]
## Date: May 2015
## Comment: Code to read Individual household electric power consumption Data Set
## and to create plot4.png file.
# Set Working Directory
setwd("~/Coursera/Exploratory Data Analysis/Week1 - PA1")
getwd()
## Setting locale Time to English from Spanish
Sys.setlocale(category = "LC_TIME", "en_US.UTF-8")
## Function to Getting and cleaning Data
getPowerData <- function(){
## Set Source Filename to load
SrcFilename <- "household_power_consumption.txt"
## Load table with data from file
dp <- read.table(SrcFilename, header = TRUE, sep = ";", na.strings = "?")
## Subsetting 2007-02-01 and 2007-02-02 data
dp <- dp[(dp$Date=="1/2/2007") | (dp$Date=="2/2/2007"),]
## Convert Date and Time variables to Date/Time classes in R
dp$DateTime <- strptime(paste(dp$Date, dp$Time), "%d/%m/%Y %H:%M:%S")
dp$Date <- as.Date(dp$Date)
return(dp)
}
powerdata <- getPowerData()
## Open Graphic Device png
png(filename="plot4.png", width=480, height=480, units="px", bg="white")
## Creating layout 2x2 for plots
par(mfrow=c(2,2))
## Figure 1: Plot for Global_active_power
plot(powerdata$DateTime, powerdata$Global_active_power, type = "l",
xlab="", ylab="Global Active Power")
## Figure 2: Plot for Voltage
plot(powerdata$DateTime, powerdata$Voltage, type="l",
xlab="datetime", ylab="Voltage")
## Figure 3: Plot for Sub_metering_1, Sub_metering_2, Sub_metering_3
plot(powerdata$DateTime, powerdata$Sub_metering_1, type = "l", xlab = "", ylab="Energy sub metering")
lines(powerdata$DateTime, powerdata$Sub_metering_2, type = "l", col = "red")
lines(powerdata$DateTime, powerdata$Sub_metering_3, type = "l", col = "blue")
## Added legends to plot
legend("topright",c("Sub_metering_1","Sub_metering_2", "Sub_metering_3"),
lty=c(1,1,1),lwd=c(1,1,1), bty="n",
col=c("black","blue","red"))
## Figure 4:
plot(powerdata$DateTime, powerdata$Global_reactive_power, type = "l",
xlab="datetime", ylab="Global_reactive_power")
## Close graphics Device
dev.off()