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
45 lines (34 loc) · 1.9 KB
/
plot1.R
File metadata and controls
45 lines (34 loc) · 1.9 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
39
40
41
42
43
44
45
# set working directory
setwd('~/Gitrs/ExData_Plotting1')
# required packages
library(data.table)
library(lubridate)
# check to see if the existing tidy data set exists; if not, make it...
if (!file.exists('household_power_consumption.txt')) {
# download the zip file and unzip
file.url<-'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
download.file(file.url,destfile='power_consumption.zip', method="curl")
unzip('power_consumption.zip',exdir='./',overwrite=F)
# read the raw table and limit to 2 days
variable.class<-c(rep('character',2),rep('numeric',7))
power.consumption<-read.table('household_power_consumption.txt',header=TRUE,
sep=';',na.strings='?',colClasses=variable.class)
power.consumption<-power.consumption[power.consumption$Date=='1/2/2007' | power.consumption$Date=='2/2/2007',]
# clean up the variable names and convert date/time fields
cols<-c('Date','Time','GlobalActivePower','GlobalReactivePower','Voltage','GlobalIntensity',
'SubMetering1','SubMetering2','SubMetering3')
colnames(power.consumption)<-cols
power.consumption$DateTime<-dmy(power.consumption$Date)+hms(power.consumption$Time)
power.consumption<-power.consumption[,c(10,3:9)]
# write a clean data set to the directory
write.table(power.consumption,file='household_power_consumption.txt',sep='|',row.names=FALSE)
} else {
power.consumption<-read.table('household_power_consumption.txt',header=TRUE,sep='|')
power.consumption$DateTime<-as.POSIXlt(power.consumption$DateTime)
}
# open device
png(filename='plot1.png',width=480,height=480,units='px')
# plot data
hist(power.consumption$GlobalActivePower,main='Global Active Power',xlab='Global Active Power (kilowatts)',col='red')
# Turn off device
x<-dev.off()