Repository navigation
Expand file tree
/
Copy pathplot4.R
More file actions
43 lines (35 loc) · 1.69 KB
/
Copy pathplot4.R
File metadata and controls
43 lines (35 loc) · 1.69 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
plot4 <- function () {
# load the data for 1-2 Feb 2007
pc1 <- loadPCdays()
# create the plot on a transparant background (is in the example provided)
par (bg='transparent', mfrow= c(2,2), cex=0.6, omi=c(0, 0.05, 0, 0))
# plot 1
plot (pc1$DateTime, pc1$Global_active_power, type='l', ylab='Global Active Power', xlab='')
#plot 2
plot (pc1$DateTime, pc1$Voltage, type='l', ylab='Voltage', xlab='datetime')
#plot 3
plot (pc1$DateTime, pc1$Sub_metering_1, type= 'n', ylab='Energy sub metering', xlab='')
points (pc1$DateTime, pc1$Sub_metering_1, type = 'l', col='black' )
points (pc1$DateTime, pc1$Sub_metering_2, type = 'l', col='red' )
points (pc1$DateTime, pc1$Sub_metering_3, type = 'l', col='blue' )
# set the legend with 'text.with' to get the full text within the legend
legend ('topright', lty=1, col = c('black', 'red', 'blue'),
legend= c('Sub_metering_1','Sub_metering_2','Sub_metering_3' ),
text.width = 72000, y.intersp=1.5, bty='n')
#plot 4
plot (pc1$DateTime, pc1$Global_reactive_power, type='l',
ylab='Global_reactive_power', xlab='datetime')
# copy to the png file with the same size as the example provided
dev.copy(png, file = "plot4.png")
dev.off()
# reset the number of rows and columns
par (mfrow= c(1,1))
}
loadPCdays <- function () {
pc0 <<- read.csv('household_power_consumption.txt', sep=';', na.strings='?')
# select the rows for Feb 1-2, 2007
pc1 <- pc0[(pc0$Date=='1/2/2007')|(pc0$Date=='2/2/2007'),]
#add DateTime column
pc1$DateTime = strptime(paste (pc1$Date,pc1$Time, sep=' '), "%d/%m/%Y %H:%M:%S", tz='')
pc1
}