forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3.R
More file actions
38 lines (22 loc) · 1.3 KB
/
Copy pathplot3.R
File metadata and controls
38 lines (22 loc) · 1.3 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
plot3 <- function(){
## Read in the data from the data set the separator is specified as ; and the
## NA values are defined as ?
data<-read.table("household_power_consumption.txt",header=TRUE, sep=";", na.strings="?")
## Merge time and date together and convert to timestamp
data$timestamp<-strptime(paste(data$Date,data$Time), format="%d/%m/%Y%H:%M:%S")
## Subset the data to chose only the two days of interest
data <- subset(data, timestamp >= "2007-02-01 00:00:00" & timestamp <= "2007-02-02 23:59:00")
## Open the PNG device to write write the graph into
png(filename = "plot3.png", width = 480, height = 480, units = "px")
## Create the plot with with
plot(data$timestamp, data$Sub_metering_1, "l", col="black", xlab="", ylab="Energy sub metering")
points(data$timestamp, data$Sub_metering_2, "l", col="red")
points(data$timestamp, data$Sub_metering_3, "l", col="blue")
## Prepare the strings for the legend and colors
leg<- c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
color<- c("black", "red", "blue")
## create legend
legend("topright", legend = leg, lwd=2, col=color )
## Close the device and return to standard device
dev.off()
}