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
28 lines (25 loc) · 1.45 KB
/
Copy pathplot3.R
File metadata and controls
28 lines (25 loc) · 1.45 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
## Exploratory Data Analysis Project 1
## Energy sub metering
## plot3.R -------> plot3.png
## Load data file and convert Date and time objects
powerdata<- read.csv("household_power_consumption.txt", sep=";",
colClasses = c("character", "character","numeric","numeric","numeric",
"numeric","numeric","numeric","numeric"),
na.strings = "?")
powerdata$Time <- strptime(paste(powerdata$Date, powerdata$Time), "%d/%m/%Y %H:%M:%S")## keep #Var at 9
powerdata$Date <- as.Date(powerdata$Date, "%d/%m/%Y")
## Create a data subset for the require time frame
Dates <- as.Date(c("2007-02-01", "2007-02-02"), "%Y-%m-%d")
pdata_subset <- subset(powerdata, Date %in% Dates)
## Date and Time and change date format
pdata_subset$Datetime <- as.POSIXct(pdata_subset$Time) ## subset aditional var for clarity
## Save plot in png format
png("plot3.png" , width = 480, height = 480) ## notice that dates are in french
## Create plot Energy sub metering vs Time
plot(pdata_subset$Datetime, pdata_subset$Sub_metering_1,type="l",
xlab="", ylab="Energy Sub Meetering")
lines(pdata_subset$Datetime, pdata_subset$Sub_metering_2, type="l",col="red")
lines(pdata_subset$Datetime, pdata_subset$Sub_metering_3, type="l",col="blue")
legend("topright", col=c("black", "red", "blue"), lty=1, lwd=2,
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
dev.off()