forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
82 lines (65 loc) · 2.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Set working directory
setwd("~/GitHub/ExData_Plotting1")
############################## Data Download ###################################
#
# # Uncomment the below to download the data to your working directory and unzip
# url <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
# destfile <- "household_power_consumption.zip"
# download.file(url, destfile)
#
# # Set a time stamp
# downloadTime<-Sys.time()
#
# # Print the time stamp to the console
# downloadTime
#
# # For the original script the file was downloaded:
# 2015-07-09 14:31:00 EDT
#
# # Unzip the file
# unzip(destfile, files = "household_power_consumption.txt")
################################################################################
############################## Read in Data ####################################
#
# Install the below packages if not installed
# data.table is excellent for dealing with large files
if("data.table" %in% rownames(installed.packages()) == FALSE) {
install.packages("data.table")
}
if("lubridate" %in% rownames(installed.packages()) == FALSE) {
install.packages("lubridate")
}
# Load required packages
library(data.table)
library(lubridate)
# Read in the data
pwr <- fread("household_power_consumption.txt", sep = ';', na.strings = '?')
# Paste Date and Time together to create DateTime field
pwr[,DateTime:=paste(Date, Time, sep = " ")]
# Change DateTime to date time format
pwr[,DateTime:=fast_strptime(DateTime, format = "%d/%m/%Y %H:%M:%S")]
# Subset data to 2007-02-01 through 2007-02-02
pwr <- pwr[as.Date(DateTime) >= '2007-02-01' & as.Date(DateTime) <= '2007-02-02',]
# Convert fields to numeric
pwr[,Global_active_power:=as.numeric(Global_active_power)]
pwr[,Global_reactive_power:=as.numeric(Global_reactive_power)]
pwr[,Voltage:=as.numeric(Voltage)]
pwr[,Global_intensity:=as.numeric(Global_intensity)]
pwr[,Sub_metering_1:=as.numeric(Sub_metering_1)]
pwr[,Sub_metering_2:=as.numeric(Sub_metering_2)]
pwr[,Sub_metering_3:=as.numeric(Sub_metering_3)]
# Change Date to date format
pwr[,Date:=fast_strptime(Date, format = "%d/%m/%Y")]
# Change Time to time format
pwr[,Time:=hms(Time)]
# Create Plot 1
# Open a png plotting environment
png(filename = "plot1.png", width = 480, height = 480, units = "px")
# Plot a histogram of Global_active_power set the color to red the title to
# 'Global Active Power' and the title of the x axis to 'Global Active Power (kilowatts)'
hist(pwr[,Global_active_power],
col = 'red',
main = 'Global Active Power',
xlab = 'Global Active Power (kilowatts)')
# Close the plotting environment
dev.off()