forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
34 lines (28 loc) · 1.27 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
library(sqldf)
##Store the data in a sql table(on disk)
##Create/connect to a database named "data_db.sqlite
con <- dbConnect(RSQLite::SQLite(), dbname = "data_db.sqlite")
#Write txt file into the database
dbWriteTable(con, name = "data_table", value = "household_power_consumption.txt",
row.names = FALSE, header = TRUE, sep = ";")
#select required dataset
df<- dbGetQuery(con, "SELECT * FROM data_table WHERE Date in ('1/2/2007', '2/2/2007')")
#create a new date_time column for Date Time in POSIXct
df$date_time<- strptime(paste(df$Date, df$Time), format="%d/%m/%Y %H:%M:%S")
#plot
png("plot4.png")
par(mfrow=c(2,2))
plot(df$date_time, df$Global_active_power, type="l",
ylab = "Global Active Power (kilowatts)")
plot(df$date_time, df$Voltage, type="l",
ylab = "Global Active Power (kilowatts)")
plot(df$date_time,df$Sub_metering_1, type="l", ylab = "Energy sub metering", xlab="")
points(df$date_time,df$Sub_metering_2, type="l", col="red")
points(df$date_time,df$Sub_metering_3, type="l", col="blue")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2", "Sub_metering_3"),
lty=1,
cex=0.8,
col = c("black","blue","red"))
plot(df$date_time, df$Global_reactive_power, type="l",
ylab = "Global Active Power (kilowatts)")
dev.off()