-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot4.R
27 lines (25 loc) · 1.63 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
#Download household_power_consumption.txt and save it in a folder. Set that folder as your working directory.
#Afterwards, run the below code. It should take around 30 seconds. The PNG file should then appear in that folder.
#loading, preprocessing and subsetting
par(mar=c(5.1, 4.1, 4.1, 2.1), mfrow = c(1,1)) #to reset the parameters
pc = read.table("household_power_consumption.txt", sep =";", header = TRUE, na.strings="?")
pc$ExactTime = as.POSIXct(paste(pc$Date, pc$Time), format="%d/%m/%Y %H:%M:%S")
lower.bound = as.POSIXct("01-02-2007", format="%d-%m-%Y")
upper.bound = as.POSIXct("03-02-2007", format="%d-%m-%Y")
subset.pc = subset(pc, ExactTime > lower.bound & ExactTime < upper.bound)
#plotting
par(mfrow=c(2,2))
#plot1
plot(subset.pc$ExactTime, subset.pc$Global_active_power, type = "l", xlab = "", ylab = "Global active power")
#plot2
plot(subset.pc$ExactTime, subset.pc$Voltage, type = "l", xlab = "datetime", ylab = "Voltage")
#plot3
plot(subset.pc$ExactTime, subset.pc$Sub_metering_1, type = "l", col = "black", xlab = "", ylab = "Energy sub metering")
lines(subset.pc$ExactTime, subset.pc$Sub_metering_2, type = "l", col = "red")
lines(subset.pc$ExactTime, subset.pc$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"), bty="n", cex=0.4, y.intersp = 0.2)
#plot4
plot(subset.pc$ExactTime, subset.pc$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global reactive power")
dev.copy(png, file = "plot4.png", width=480, height=480)
dev.off()
cat("plot4.png is generated and is stored in your current directory:",getwd())