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
87 lines (70 loc) · 4.51 KB
/
plot4.R
File metadata and controls
87 lines (70 loc) · 4.51 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
########################################################################################
##
## To perform analysis for this assignment, it is necessary to download dataset from URL:
## https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
##
## The code below checks if the necessary data file exists in the working directory. If not,
## it will be downloaded from the web and unzipped to your working directory.
##
## If you want to prevent downloading, make sure that you retrieve and copy a file named
## "household_power_consumption.txt" to the working directory.
##
########################################################################################
plot4 <- function() {
## Load data.table package needed in order to use 'fread' function for fast file reading
## If it isn't installed, install the data.table package with install.packages()
library(data.table)
## Set the name of data file
file <- "household_power_consumption.txt"
## Check if the file exists in the working directory. If not, retrieve it from the web
if (!file.exists(file)) {
fileURL <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(fileURL, "data.zip", method = "curl")
unzip("data.zip")
}
## 'fread' function does the same as 'read.table', but faster.
## Here it reads a file in table format where all variables are read in as characters
alldata <- fread(file, header = TRUE, sep = ";", na.strings = "?", colClasses = "character")
## Subset data to the required dates for analysis
data <- alldata[alldata$Date == "1/2/2007" | alldata$Date == "2/2/2007",]
## Create a POSIXlt/POSIXct class vector of the concatenated values of Date and Time character vectors
datetime <- strptime(paste(data$Date, data$Time), format = "%d/%m/%Y %H:%M:%S")
## Set PNG graphics device and create output file
png("plot4.png", width = 480, height = 480, units = "px")
## Set the layout of multiplot filled by columns (mfcol)
par(mfcol = c(2,2))
## Plot topleft
plot(datetime, as.numeric(data$Global_active_power),
type = "l", ## set the type of plot ("l" for lines)
xlab = "", ## set the label for x-axis (empty as in the reference plot)
ylab = "Global Active Power") ## set the label for y-axis
## Plot bottomleft
plot(datetime, as.numeric(data$Sub_metering_1), ## Plot data only for Sub_metering_1 variable
type = "l", ## set the type of plot ("l" for lines)
xlab = "", ## set the label for x-axis (empty as in the reference plot)
ylab = "Energy sub metering") ## set the label for y-axis
# Add data for Sub_metering_2 variable to the plot (color - red, type - lines)
points(datetime, as.numeric(data$Sub_metering_2), col = "red", type = "l")
# Add data for Sub_metering_3 variable to the plot (color - blue, type - lines)
points(datetime, as.numeric(data$Sub_metering_3), col = "blue", type = "l")
# Create legend for the plot
legend("topright", ## set the position of the legend in the plot
legend = names(data)[7:9], ## set the character vector to be shown in the legend (names of the plotted variabels)
col = c("black", "red", "blue"), ## set the color of symbol/lines shown in the legend
lty = 1, ## set the simbol to be shown in the legend (lty = 1 for solid line)
bty = "n", ## set the type of box to be drawn around the legend ("n" for none)
cex = 0.95) ## set the size of the legend relative to the default value
## (slightly reduced to match the reference plot)
## Plot topright
plot(datetime, as.numeric(data$Voltage),
type = "l", ## set the type of plot ("l" for lines)
xlab = "datetime", ## set the label for x-axis
ylab = "Voltage") ## set the label for y-axis
## Plot bottomright
plot(datetime, as.numeric(data$Global_reactive_power),
type = "l", ## set the type of plot ("l" for lines)
xlab = "datetime", ## set the label for x-axis
ylab = "Global_reactive_power") ## set the label for y-axis
## Close graphics device
dev.off()
}