forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.R
More file actions
74 lines (61 loc) · 2.7 KB
/
plot2.R
File metadata and controls
74 lines (61 loc) · 2.7 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
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
workdir <- "C:/Users/aahumada.SA/MatLab/RStudio/ExData_Plotting1"
src1 <- paste(workdir,"household_power_consumption.txt" , sep = "/")
setwd(workdir)
#------------------------------------------
# LOAD DATA SOURCE
#------------------------------------------
# Date : Date in format dd/mm/yyyy
# Time : time in format hh:mm:ss
# Global_active_power : household global minute-averaged active power (in kilowatt)
# Global_reactive_power : household global minute-averaged reactive power (in kilowatt)
# Voltage : minute-averaged voltage (in volt)
# Global_intensity : household global minute-averaged current intensity (in ampere)
# Sub_metering_1 : energy sub-metering No. 1 (in watt-hour of active energy).
# It corresponds to the kitchen, containing mainly a dishwasher,
# an oven and a microwave (hot plates are not electric but gas powered).
# Sub_metering_2 : energy sub-metering No. 2 (in watt-hour of active energy).
# It corresponds to the laundry room, containing a washing-machine,
# a tumble-drier, a refrigerator and a light.
# Sub_metering_3 : energy sub-metering No. 3 (in watt-hour of active energy).
# It corresponds to an electric water-heater and an air-conditioner.
#------------------------------------------
# EXAMPLE
#------------------------------------------
# Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3
# 16/12/2006;17:24:00;4.216;0.418;234.840;18.400;0.000;1.000;17.000
data <- read.table(src1
, header=TRUE
, sep=";"
, colClasses = "character"
)
#
# Generate datetime, convert to date and numeric
#
data$dt <- strptime(paste(data[,1],data[,2]),"%d/%m/%Y %H:%M:%S")
data[,1] <- as.Date( data[,1], "%d/%m/%Y")
for ( i in c(3:9) ) { data[,i] <- as.numeric(data[,i]) }
#
# Subset from 2007-02-01 to 2007-02-02
#
d.ini <- as.Date("2007-02-01")
d.end <- as.Date("2007-02-02")
cond <- data$Date >= d.ini & data$Date <= d.end
#------------------------------------------
# DRAW GRAPHICS
#------------------------------------------
png( file ="plot2.png"
,width = 504
,height = 504
,bg = "transparent"
)
opar <- par(no.readonly=TRUE)
with(subset(data, cond ) , {
plot(dt, Global_active_power
, type="l"
, ann=FALSE
)
title( ylab="Global Active Power (kilowatts)" )
}
)
par(opar)
dev.off() ## Don't forget to close the PNG device!