forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
34 lines (27 loc) · 1.1 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
file.url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
zip.file.name <- "household_power_consumption.zip"
file.name <- "household_power_consumption.txt"
if (!file.exists(file.name))
{
# download the dataset
download.file(file.url, destfile=zip.file.name, method="curl")
#unzip the dataset
unzip(zip.file.name)
}
# read the dataset
data <- read.csv(file.name, sep=";",
colClasses="character", header=TRUE)
# merge Date and Time columns to one column named DateTime in the POSIXlt format
data <- transform(data, Time=paste(Date, Time))
data <- transform(data, Time=strptime(Time, format="%d/%m/%Y %H:%M:%S"))
data <- data[,2:9]
colnames(data)[1] <- "DateTime"
# filter the dataset according to the desired time period
data <- subset(data, as.Date(data$DateTime) >= as.Date("2007-02-01") &
as.Date(data$DateTime) <= as.Date("2007-02-02"))
#plot the histogram
with(data, hist(as.numeric(Global_active_power), col="red",
xlab="Global Active Power (kilowatts)", main="Global Active Power"))
# save png file
dev.copy(png, file="plot1.png")
dev.off()