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
38 lines (31 loc) · 1.86 KB
/
plot1.R
File metadata and controls
38 lines (31 loc) · 1.86 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
32
33
34
35
36
37
38
RetrieveDataFunc <- function()
{
download.file(url="https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip", destfile="exdata_data_household_power_consumption.zip")
unzip("exdata_data_household_power_consumption.zip")
}
print (paste("Running in", getwd()))
print ("Files will be retrieved to this directory, and you may wish to remove them when done.")
#Checks if data file exists, if not calls RetrieveDataFunc to download and extract dataset.
if ( !file.exists("household_power_consumption.txt"))
{ print ("Data files were not found, retrieving files...")
RetrieveDataFunc()
print ("Files retrieved.")
}else{
print ("Found data files, skipping download step.")
}
print ("Loading the data...")
#Reads in the data with read.table
PowerConsumption <- read.table(file="household_power_consumption.txt", sep=";", header=TRUE, na.strings="?", stringsAsFactors=FALSE)
print ("Data loaded, creating subset of data for specific dates")
#Creates a subset of the data to the specific dates of interest.
PowerConsumptionSub <- subset (PowerConsumption, subset= PowerConsumption$Date %in% c("1/2/2007", "2/2/2007"))
print ("Data subset created for dates between 2007-02-01 and 2007-02-02")
#Adds an additional TimeStamp field to the data subset, combines date and time, and converts it to an R datetime format.
print ("Converting dates and times to timestamp in R date format.")
PowerConsumptionSub$TimeStamp <- paste(PowerConsumptionSub$Date, PowerConsumptionSub$Time)
PowerConsumptionSub$TimeStamp <- strptime(PowerConsumptionSub$TimeStamp, format = "%d/%m/%Y %H:%M:%S")
#Generates plot1.png
png(filename="plot1.png", width=480, height=480, type="cairo")
hist(PowerConsumptionSub$Global_active_power, col="RED", main="Global Active Power", xlab="Global Active Power (kilowatts)", ylab="Frequency")
dev.off()
print (paste(getwd(),"/plot1.png has been created.", sep=""))