forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot1.R
More file actions
31 lines (27 loc) · 1.38 KB
/
plot1.R
File metadata and controls
31 lines (27 loc) · 1.38 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
# The script reads the Electric Power Consumption dataset, obtains a subset,
# and generates a histogram as 'plot1.png' file as required.
#
# PRE-REQUISITES:
# (1) This script expects the zipped file 'exdata-data-household_power_consumption.zip'
# in the working directory.
# (2) Sourcing and executing the function gen_plot1() without any parameters.
#
gen_plot1 <- function() {
# read the dataset after unzipping the file
powerdata <- read.table(unz("exdata-data-household_power_consumption.zip",
"household_power_consumption.txt"),
header = TRUE, sep = ";", stringsAsFactors = FALSE, na.strings="?");
# rename variable names for ease of processing and readability
names(powerdata) <- tolower(gsub("_", "", names(powerdata)));
# subset the data only for two days worth of observations i.e. dates 2007-02-01 and 2007-02-02
reqdata <- subset(powerdata, date == "1/2/2007" | date == "2/2/2007");
## generate the PNG file with the required plot
# open the PNG device with a custom background
png(file = "plot1.png", bg = "white");
# generating plot with custom color, title, and X-axis label
hist(reqdata$globalactivepower, col = "red", main = "Global Active Power",
xlab="Global Active Power (kilowatts)");
# closing the PNG device
dev.off();
print("Plot generated on file: plot1.png")
}