forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
33 lines (25 loc) · 1.45 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
# read only 2 day data
data <- read.csv("household_power_consumption.txt",header = TRUE, sep=";", na.strings="?", skip=66636, nrows=2880)
#name columns properly
colnames(data) <- c("Date","Time","Global_active_power","Global_reactive_power", "Voltage", "Global_intensity", "Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
# launch png graphics device
png(filename = "plot4.png", width = 480, height = 480, units = "px")
# create plots
par(mfrow = c(2, 2))
# first plot
plot(as.numeric(data$Global_active_power),type="l", main="", ylab = "Global Active Power", xaxt = "n", xlab ="")
axis(1,at = c(1,1440,2880),labels = c("Thu", "Fri", "Sat"))
# second plot
plot(as.numeric(data$Voltage),type="l", main="", ylab = "Voltage", xaxt = "n", xlab ="datetime")
axis(1,at = c(1,1440,2880),labels = c("Thu", "Fri", "Sat"))
# third plot
plot(as.numeric(data$Sub_metering_1),type="l", main="", ylab = "Energy sub metering", xaxt = "n", xlab ="")
points(as.numeric(data$Sub_metering_2),type="l",col = "red")
points(as.numeric(data$Sub_metering_3),type="l",col = "blue")
axis(1,at = c(1,1440,2880),labels = c("Thu", "Fri", "Sat"))
legend("topright", c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lty=c(1,1,1),col=c("black","red", "blue"), bty = "n")
# last plot
plot(as.numeric(data$Global_reactive_power),type="l", main="", ylab = "Global_reactive_power", xaxt = "n", xlab ="datetime")
axis(1,at = c(1,1440,2880),labels = c("Thu", "Fri", "Sat"))
# close graphics device
dev.off()