forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
49 lines (40 loc) · 1.76 KB
/
plot3.R
File metadata and controls
49 lines (40 loc) · 1.76 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
Date1 <- strptime("01/02/2007","%d/%m/%Y") # first date in data to analyse
Date2 <- strptime("02/02/2007","%d/%m/%Y") # second date in data to analyse
## Read whole data
data <- read.csv2("household_power_consumption.txt",
stringsAsFactors = FALSE,
na.strings = "?")
## use only data for specified dates
data <- data[strptime(data$Date, "%d/%m/%Y") == Date1 |
strptime(data$Date, "%d/%m/%Y") == Date2,]
## In order to use a combination of date and time
## (there are only two days, so date alone wouldn't produce sufficient data')
## we use a combination of date and time
## first convert character to date ...
data$Date <- strptime(data$Date, "%d/%m/%Y")
## ... then add time to it (using strptime does not work), creating a new combination Moments
data$Moments <- as.POSIXct(paste(data$Date, data$Time))
png("plot3.png", width=480, height= 480) ## open png device
plot(data$Moments, # Date-and-Time-Kombinatiomn
data$Sub_metering_1, # Data to plot
type= "n", # draw frame only, no data
ylab= "Energy sub metering", # y-axis text
xlab="") # no x-axis text
points(data$Moments,
data$Sub_metering_1, # actual data
type= "l", # plottinng lines
col="black") # black color
points(data$Moments,
data$Sub_metering_2, # actual data
type= "l", # plotting lines
col="red") # red color
points(data$Moments,
data$Sub_metering_3, # actual data
type= "l", # plotting lines
col="blue") # blue color
legend("topright",
c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), # legend texts
lty=c(1,1,1), # lines
lwd=c(2.5,2.5,2.5), # line length
col=c("black","red","blue")) # line colors
dev.off() ## close device