forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
27 lines (18 loc) · 863 Bytes
/
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
# Import helper function to download file
source("get_file.R")
file <- get_file("https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", "./data", "data.zip", unzip = TRUE)
# Extract the file
unzip(file)
data_file_path <- file.path("./household_power_consumption.txt")
# Read the raw data into data
data <- read.table(data_file_path, header = TRUE, sep = ";", colClasses = c(rep("character",2), rep("numeric",7)), na.strings = "?")
# Use dplyr for data manipulation
library(dplyr)
d <- tbl_df(data)
rm("data")
# Filter only the specified date range
selected_date_data <- filter(d, Date == "2/2/2007" | Date == '1/2/2007')
# Plot the image and save it as plot1.png
png('./plot1.png')
hist(selected_date_data$Global_active_power, col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")
dev.off()