forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot4.R
More file actions
42 lines (34 loc) · 1.71 KB
/
Copy pathplot4.R
File metadata and controls
42 lines (34 loc) · 1.71 KB
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
41
42
elec<-read.table("exdata-data-household_power_consumption\\household_power_consumption.txt", sep=";", skip=1,
col.names=c("date", "time", "active_power", "inactive_power", "voltage",
"intensity", "meter1", "meter2", "meter3"),
colClasses=c("character", "character", "character","character", "character",
"character","character", "character", "character"));
sub1<-subset(elec, date=="1/2/2007");
sub2<-subset(elec, date=="2/2/2007");
sub<-rbind(sub1, sub2);
par(mfrow=c(2,2));
#png("plot4.png",width = 480, height = 480);
dev.set(2);
# plot 2
active_power<-as.numeric(sub$active_power);
plot(active_power, type="l", xaxt="n", xlab="datetime", ylab="Global Active Power (kilowatts)");
axis(1, at=c(1,length(sub$date)/2,length(sub$date)), labels=c("Thu","Fri", "Sat"));
# voltage
#voltage<-as.numeric(sub$voltage);
plot(sub$voltage, type="n",xaxt="n", ylab="Voltage", xlab="datetime")
lines(sub$voltage)
axis(1, at=c(1,length(sub$date)/2,length(sub$date)), labels=c("Thu","Fri", "Sat"));
# plot 3
plot(sub$meter1, type="n",xaxt="n", xlab="datetime", ylab="Energy sub metering" )
lines(sub$meter1, col="black");
lines(sub$meter2, col="red");
lines(sub$meter3, col="blue");
legend("topright", c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), lty=c(1,1), col=c("black", "red", "blue"));
axis(1, at=c(1,length(sub$date)/2,length(sub$date)), labels=c("Thu","Fri", "Sat"));
# reactive power
plot(sub$inactive_power, type="n",xaxt="n", ylab="Global_reactive_power", xlab="datetime")
lines(sub$inactive_power)
axis(1, at=c(1,length(sub$date)/2,length(sub$date)), labels=c("Thu","Fri", "Sat"));
dev.copy(png,file="plot4.png");
dev.off();
#dev.off();