forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot3.R
66 lines (54 loc) · 3.44 KB
/
plot3.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
64
65
66
########################################################################################
##
## 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.
##
########################################################################################
plot3 <- 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("plot3.png", width = 480, height = 480, units = "px")
## Plot data only for Sub_metering_1 variable
plot(datetime, as.numeric(data$Sub_metering_1),
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 generated above
## 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 generated above
## 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
lty = 1, ## set the simbol to be shown in the legend (lty = 1 for solid line)
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
cex = 0.95) ## set the size of the legend relative to the default value
## (slightly reduced to match the reference plot)
## Close graphics device
dev.off()
}