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
39 lines (36 loc) · 2.04 KB
/
Copy pathplot4.R
File metadata and controls
39 lines (36 loc) · 2.04 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
## Exploratory Data Analysis Project 1
## Multi plot
## plot4.R -------> plot4.png
## Load data file and convert Date and time objects
powerdata<- read.csv("household_power_consumption.txt", sep=";",
colClasses = c("character", "character","numeric","numeric","numeric",
"numeric","numeric","numeric","numeric"),
na.strings = "?")
powerdata$Time <- strptime(paste(powerdata$Date, powerdata$Time), "%d/%m/%Y %H:%M:%S")## keep #Var at 9
powerdata$Date <- as.Date(powerdata$Date, "%d/%m/%Y")
## Create a data subset for the require time frame
Dates <- as.Date(c("2007-02-01", "2007-02-02"), "%Y-%m-%d")
pdata_subset <- subset(powerdata, Date %in% Dates)
## Date and Time and change date format
pdata_subset$Datetime <- as.POSIXct(pdata_subset$Time) ## subset aditional var for clarity
## Save plot in png format
png("plot4.png" , width = 480, height = 480) ## notice that dates are in french
## Create a multiplot of 4 plots
par(mfrow=c(2,2),mar=c(4,4,2,1), oma=c(0,0,2,0))
###-------------------------------------------1
plot(pdata_subset$Datetime, pdata_subset$Global_active_power, type="l",
ylab="Global Active Power (kilowatts)", xlab="")
###-------------------------------------------2
plot(pdata_subset$Datetime, pdata_subset$Voltage, type="l",
ylab="Voltage", xlab="datetime")
###-------------------------------------------3
plot(pdata_subset$Datetime, pdata_subset$Sub_metering_1,type="l",
xlab="", ylab="Energy Sub Meetering")
lines(pdata_subset$Datetime, pdata_subset$Sub_metering_2, type="l",col="red")
lines(pdata_subset$Datetime, pdata_subset$Sub_metering_3, type="l",col="blue")
legend("topright", col=c("black", "red", "blue"), lty=c(1,1,1),lwd=c(2.5,2.5,2.5), bty="n",
legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
###-------------------------------------------4
plot(pdata_subset$Datetime, pdata_subset$Global_reactive_power, type="l",
ylab="Global Reactive Power (kilowatts)", xlab="datetime")
dev.off()