forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
59 lines (51 loc) · 1.12 KB
/
plot1.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
# Configuration
fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
destDir <- "./data"
destZipfile <- "./data/power_consumption.zip"
destFile <- "./data/household_power_consumption.txt"
# Create data directory if needed
if (!file.exists(destDir)) {
dir.create(destDir)
}
# Download and extract data if needed
if (!file.exists(destfile)) {
# Download
download.file(fileUrl, destfile)
# Extract
unzip(destfile, exdir = destDir)
}
# Read the data
f <- file(destFile, "r");
data <- read.table(
text = grep("^[1,2]/2/2007", readLines(f), value = TRUE),
sep = ";",
skip = 0,
na.strings = "?",
stringsAsFactors = FALSE
)
# Name the columns
names(data) <- c(
"date",
"time",
"active_power",
"reactive_power",
"voltage",
"intensity",
"sub_metering_1",
"sub_metering_2",
"sub_metering_3"
)
# Plot - I
par(mfrow = c(1, 1))
hist(
data$active_power,
main = "Global Active Power",
xlab = "Global Active Power (kilowatts)",
ylab = "Frequency",
col = "red",
freq = TRUE
)
# Create output file
dev.copy(png, file = "plot1.png")
# Close
dev.off()