forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot3.R
33 lines (24 loc) · 1.19 KB
/
plot3.R
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
## Plot the dependences of energy sub metering on time
# read data (please save file household_power_consumption.txt in "./data")
data <- read.csv("./data/household_power_consumption.txt", sep=";", stringsAsFactors=FALSE)
#what is the memory size used by data?
object.size(data)
#filter data by date and save it in new data frame df
df<-data[data$Date=="1/2/2007"|data$Date=="2/2/2007",]
#what is the memory size used by df?
object.size(df)
#remove the object data to free memory
rm(data)
# Concatenate Date and Time and convert the resulting vector DT to class "POSIXlt"
df$DT<-strptime(paste(df$Date, df$Time, sep=" "), format = "%d/%m/%Y %H:%M:%S")
#My locale is non-english therefore I set LC_TIME for US
Sys.setlocale("LC_TIME", "US")
#plot graphic and save it in png-file
png(filename="plot3.png")
with(df, plot(DT,as.numeric(Sub_metering_1), type="l", xlab="", ylab="Energy sub metering" ))
with(df, lines(DT,as.numeric(Sub_metering_2), type="l", col="red"))
with(df, lines(DT,as.numeric(Sub_metering_3), type="l", col="blue"))
legend("topright", lty=1, col=c("black","red", "blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
dev.off()
#restore my locale
Sys.setlocale("LC_TIME", "")