forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
27 lines (26 loc) · 1.21 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
prepareFile <- function(filename){
require(sqldf) # install.packages("sqldf") if missing
df <- read.csv.sql(file = filename,header = TRUE, sql = "select * from file where Date='1/2/2007' or Date='2/2/2007'",sep=";")
df[,10] <- paste(df$Date,df$Time,sep=' ')
dates <- strptime(df[,10],"%d/%m/%Y %H:%M:%S")
df$fulldate = dates
df
}
generatePlot4<- function(df){
par(mfrow=c(2,2))
plot(df$fulldate,df$Global_active_power,type="n",ylab = "Global active power (kilowatts)", xlab = '')
lines(df$fulldate,df$Global_active_power)
plot(df$fulldate,df$Voltage,type="n",ylab = "Voltage", xlab = 'datetime')
lines(df$fulldate,df$Voltage)
plot(x=df$fulldate,y=df$Sub_metering_1, type="n",ylab = "Energy sub metering", xlab = '')
lines(df$fulldate,df$Sub_metering_1,col="Black")
lines(df$fulldate,df$Sub_metering_2,col="Red")
lines(df$fulldate,df$Sub_metering_3,col="Blue")
legend("topright",legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), col = c("Black","Red","Blue"), lty=c(1,1))
plot(df$fulldate,df$Global_reactive_power,type="n", xlab = 'datetime')
lines(df$fulldate,df$Global_reactive_power)
}
df <- prepareFile("household_power_consumption.txt")
png("plot4.png")
generatePlot4(df)
dev.off()