forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
39 lines (30 loc) · 1.46 KB
/
plot4.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
34
35
36
37
38
39
## Multiple plots
# 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="plot4.png")
par(mfrow=c(2,2))
with(df,{
plot(DT,as.numeric(Global_active_power), type="l", xlab="", ylab="Global active power")
plot(DT,as.numeric(Voltage), type="l", xlab="datetime", ylab="Voltage" )
plot(DT,as.numeric(Sub_metering_1), type="l", xlab="", ylab="Energy sub metering" )
legend("topright", lty=1, col=c("black","red", "blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), bty='n')
lines(DT,as.numeric(Sub_metering_2), type="l", col="red")
lines(DT,as.numeric(Sub_metering_3), type="l", col="blue")
plot(DT,as.numeric(Global_reactive_power), type="l", xlab="datetime", ylab="Global_reactive_power" )
})
dev.off()
#restore my locale
Sys.setlocale("LC_TIME", "")