forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
78 lines (63 loc) · 1.73 KB
/
Copy pathplot4.R
File metadata and controls
78 lines (63 loc) · 1.73 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
73
74
75
76
77
78
columnTypes <- c('character', 'character', 'numeric', 'numeric','numeric', 'numeric', 'numeric', 'numeric', 'numeric')
alldata <- read.delim(
'household_power_consumption.txt',
sep = ';',
header = TRUE,
colClasses=columnTypes,
na.strings='?',
)
data <- subset(alldata, Date == '1/2/2007' | Date == '2/2/2007')
# easier to plot with unix time than to work with date and time separately
# merge date and time character columns and convert to unix time
unix_time <- as.numeric(as.POSIXct(with(data, paste(Date, Time)), format='%d/%m/%Y %H:%M:%S'))
days <- c('1/2/2007 00:00:00', '2/2/2007 00:00:00', '3/2/2007 00:00:00')
day_labels <- c('Thursday', 'Friday', 'Saturday')
tck_locations <- as.numeric(as.POSIXct(days, format='%d/%m/%Y %H:%M:%S'))
png(filename = 'plot4.png')
par(mfcol = c(2,2))
plot(
unix_time,
data$Global_active_power,
type = 'l',
xlab = '',
xaxt="n", # dont plot x axis ticks
ylab = 'Global Active Power (kilowatts)'
)
axis(1, at=tck_locations, labels=day_labels)
plot(
unix_time,
data$Sub_metering_1,
type = 'l',
xlab = '',
xaxt="n", # dont plot x axis ticks
ylab = 'Energy sub metering'
)
with(data, {
lines(unix_time, Sub_metering_2, col = 'red')
lines(unix_time, Sub_metering_3, col='blue')
})
axis(1, at=tck_locations, labels=day_labels)
legend(
'topright',
lty = 1,
bty = 'n',
col = c('black', 'red', 'blue'),
legend = c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3')
)
with(data, plot(
unix_time,
Voltage,
xaxt = 'n',
xlab = 'datetime',
type = 'l'
))
axis(1, at=tck_locations, labels=day_labels)
with(data, plot(
unix_time,
Global_reactive_power,
xaxt = 'n',
xlab = 'datetime',
type = 'l'
))
axis(1, at=tck_locations, labels=day_labels)
dev.off()