forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
136 lines (109 loc) · 2.95 KB
/
plot4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
################################################################################
# retreive the data
################################################################################
url = "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
tmp = tempfile()
download.file(url, tmp)
unzip(tmp)
unlink(tmp)
household_power = read.csv(
"household_power_consumption.txt",
sep = ";"
)
################################################################################
# reshape the data
################################################################################
household_power$Date =
as.Date(household_power$Date, format='%d/%m/%Y')
# only select data for two days
household_power = subset(household_power,
Date == '2007-02-01' |
Date == '2007-02-02'
)
household_power$DateTime =
as.POSIXct(
paste(household_power$Date, household_power$Time),
format="%Y-%m-%d %H:%M:%S"
)
# remove rows with missing values
household_power = subset(household_power,
Global_active_power != '?')
# num of digits to use when converting to decimal field
options(digits=9)
# convert to the correct data type
household_power$Global_active_power =
as.numeric(
as.character(household_power$Global_active_power)
)
# convert to the correct data type
household_power$Global_reactive_power =
as.numeric(
as.character(household_power$Global_reactive_power)
)
# convert to the correct data type
household_power$Voltage =
as.numeric(
as.character(household_power$Voltage)
)
# convert to the correct data type
household_power$Sub_metering_1 =
as.numeric(
as.character(household_power$Sub_metering_1)
)
household_power$Sub_metering_2 =
as.numeric(
as.character(household_power$Sub_metering_2)
)
household_power$Sub_metering_3 =
as.numeric(
as.character(household_power$Sub_metering_3)
)
################################################################################
# draw the plot
################################################################################
png(filename = "plot4.png",
width = 480, height = 480, units = "px", pointsize = 12
)
par(mfrow = c(2,2))
attach(household_power)
# plot 1
plot(
DateTime,
Global_active_power,
xlab = "",
ylab = "Global Active Power",
type = "l"
)
# plot 2
plot(
DateTime,
Voltage,
xlab = "datetime",
ylab = "Voltage",
type = "l"
)
# plot 3
plot(
DateTime,
Sub_metering_1,
xlab = "",
ylab = "Energy sub metering",
type = "n"
)
lines(DateTime, Sub_metering_1, type = 'l', col='black')
lines(DateTime, Sub_metering_2, type = 'l', col='red')
lines(DateTime, Sub_metering_3, type = 'l', col='blue')
legend(x="topright",
c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'),
lty=c(1,1,1),
col=c('black', 'red', 'blue')
)
# plot 4
plot(
DateTime,
Global_reactive_power,
xlab = "datetime",
type = "l"
)
detach(household_power)
dev.off()