forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
40 lines (32 loc) · 2.13 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
40
#Read data set
household.dataset=read.table("./household_power_consumption.txt", sep=";", header=T, colClasses="character", na.string="?")
#Convert Date column in date format
household.dataset$Date=as.Date(household.dataset$Date, "%e/%m/%Y")
#Filter dataset to include observation only for the days '2007-02-01' and '2007-02-02'
household.filt=household.dataset[household.dataset$Date %in% as.Date(c('2007-02-01','2007-02-02')),]
#Add another column with date + time and change format Global active power column in numeric
household.filt$dateTime <- strptime(paste(household.filt$Date, household.filt$Time), format='%Y-%m-%e %H:%M:%S')
household.filt$Global_active_power=as.numeric(household.filt$Global_active_power)
#convert in "numeric" the columns for the plot
household.filt$Sub_metering_1=as.numeric(household.filt$Sub_metering_1)
household.filt$Sub_metering_2=as.numeric(household.filt$Sub_metering_2)
household.filt$Sub_metering_3=as.numeric(household.filt$Sub_metering_3)
household.filt$Global_active_power=as.numeric(household.filt$Global_active_power)
household.filt$Voltage=as.numeric(household.filt$Voltage)
household.filt$Global_reactive_power=as.numeric(household.filt$Global_reactive_power)
#set english language for x axis
Sys.setlocale(category = "LC_ALL", locale = "C")
png("plot4.png", font="Times", bg="transparent")
par(mfrow=c(2,2))
#plot first plot
plot(household.filt$dateTime, household.filt$Global_active_power, type="l", ylab="Global Active Power", xlab="")
#plot second plot
plot(household.filt$dateTime, household.filt$Voltage, type="l", xlab="datetime", ylab="Voltage")
#plot third plot
plot(household.filt$dateTime, household.filt$Sub_metering_1, type="l",ylab="Energy sub metering", xlab="")
points(household.filt$dateTime, household.filt$Sub_metering_2, type="l", col="red")
points(household.filt$dateTime, household.filt$Sub_metering_3, type="l", col="blue")
legend("topright", legend=c("Submetering_1", "Submetering_2", "Submetering_3"), bty="n",col=c("black", "red", "blue"), lty=1)
#plot fourth plot
plot(household.filt$dateTime, household.filt$Global_reactive_power, type="l", xlab="datetime", ylab="Global_reactive_power")
dev.off()