forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
97 lines (92 loc) · 3.66 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Program: plot1.R
# Author: Ricardo Rodriguez 2015/04
#
# Dataset:
# Lichman, M. (2013). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml].
# Irvine, CA: University of California, School of Information and Computer Science.
#
# https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
#
#
# Modify this line to use your own directory path
# NOTE: (dWorkingDir)
# In blank would be your actual working directory
#
dWorkingDir <- ""
dWorkingDir <- "C:/ricardor/Coursera/Exploratory_Data_Analysis/ExData_Plotting1"
I_Data <- "/data"
I_export_Data <- "/data"
I_export_Plot <- "/figure_replica"
#
# I_Programs <- "Your own directory path where the scripts are stored"
# This directory cointains additional functions
#
I_Programs <- "/programs"
I_Programs <- paste0(dWorkingDir,I_Programs)
#
I_Directory <- getwd() ## Saves the actual working directory
# =================================================================================
# Loading auxiliary scripts with functions
# =================================================================================
source(paste0(I_Programs,"/dirFunctions.R"))
source(paste0(I_Programs,"/Files_mdl.R"))
# =================================================================================
# Validate Directory structure.
# Create directories if necessary.
# Data Directory
# Data Export Directory
# =================================================================================
source(paste0(I_Programs,"/set_directories.R"))
# =================================================================================
# Working with the pre-processed Data
# =================================================================================
cFile2Read <- paste0(dWorkingDir,I_Data,'/household_power_consumption.txt')
epc <- read.table(cFile2Read, header= TRUE, sep = ",")
# sapply(epc,class)
#
# =================================================================================
# Conversion of the Date and Time Columns
# Time Adds the Record Date
# =================================================================================
#head(epc$Time)
epc$Time <- strptime(paste(epc$Date,epc$Time),format = "%d/%m/%Y %H:%M:%S", tz = "")
# head(epc$Date)
epc$Date <- as.Date(epc$Date,"%d/%m/%Y")
# class(epc$Date)
# sapply(epc,class)
# =================================================================================
# Creating the plot
# =================================================================================
exp_plot <- paste0(dWorkingDir,I_export_Plot,'/plot1.png')
exp_plot <- paste0(dWorkingDir,'/plot1.png')
#
# png(exp_plot)
#
# V_par <- par()
hist(as.numeric(epc$Global_active_power),
col="red",
ps = 9,
xlab = "Global Active Power (kilowatts)",
main = "Global Active Power")
title(main = list('Plot 1',cex = 1.5),outer = TRUE, adj = 0.0)
# par(V_par)
# =================================================================================
# Copy the plot to the file
# =================================================================================
dev.copy(png,exp_plot, width=480, heigh = 480)
dev.off()
# =================================================================================
# Restore the previous Working Directory
# =================================================================================
setwd(I_Directory)
rm(epc)
rm(list=ls(pattern='I_'))
rm(list=ls(pattern='c'))
rm(list=ls(pattern='d'))
rm(list=ls(pattern='e'))
rm(list=ls(pattern='f'))
rm(list=ls(pattern='m'))
rm(list=ls(pattern='v'))
# =================================================================================
# End of the Script
# =================================================================================