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
54 lines (34 loc) · 1.87 KB
/
plot4.R
File metadata and controls
54 lines (34 loc) · 1.87 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
43
44
45
46
47
48
49
50
51
52
53
54
main <- function() {
## Will be useful later on
Sys.setlocale("LC_TIME", "en_US.UTF-8")
## Get the files in the script folder
files <- list.files()
## Check if dataset is here
if(!("household_power_consumption.txt" %in% files)) {
stop("Dataset not in script folder")
}
## Read data
data <- read.table("household_power_consumption.txt", sep=";", header=TRUE, nrows=2075259)
## Subset it
data <- subset(data, data$Date == "1/2/2007" | data$Date == "2/2/2007")
## Convert it
data$Time <- strptime(paste(data$Date,data$Time), format="%d/%m/%Y%H:%M:%S")
data[,1] <- as.Date(data[,1],format="%d/%m/%Y")
data[,3] <- as.numeric(as.character(data[,3]))
data[,7] <- as.numeric(as.character(data[,7]))
data[,8] <- as.numeric(as.character(data[,8]))
data[,9] <- as.numeric(as.character(data[,9]))
data[,5] <- as.numeric(as.character(data[,5]))
data[,4] <- as.numeric(as.character(data[,4]))
## Now we can plot it !
png(file="plot4.png")
with(data, par(mfrow=c(2,2)))
with(data, plot(y=Global_active_power,x=Time, type="l",col="black", xlab="", ylab="Global Active Power (kilowatts)"))
with(data, plot(y=data$Voltage, x=data$Time, type="l", ylab="Voltage", xlab="datetime"))
with(data, plot(y=data$Sub_metering_1,x=data$Time, type="l",col="black", ylab="Energy sub metering", xlab=""))
with(data, lines(y=data$Sub_metering_2, x=data$Time, type="l", col="red"))
with(data, lines(y=data$Sub_metering_3, x=data$Time, type="l", col="blue"))
with(data, legend("topright",legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), col=c("black","red","blue"), pch=c("-","-","-")))
with(data, plot(y=data$Global_reactive_power, x=data$Time, type="l", col="black", xlab="datetime"))
dev.off()
}