forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot4.R
41 lines (31 loc) · 1.95 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
34
35
36
37
38
39
40
41
# Download the zip file that contains the data set
data_url <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
zip_file <- 'dataset.zip'
download.file(data_url, destfile = zip_file, method = 'curl')
unzip(zip_file)
# Load data set to memory
power_consumption <- read.table("./household_power_consumption.txt", header=TRUE, sep=";", na.strings = "?")
#str(power_consumption)
# Cast power_consumption$Date to Date
power_consumption$Date <- as.Date(power_consumption$Date, format="%d/%m/%Y")
# Subset data from first two days of February 2007
consumption_february <- power_consumption[(power_consumption$Date=="2007-02-01") | (power_consumption$Date=="2007-02-02"),]
#Add new column with timestamp info
consumption_february <- transform(consumption_february, timestamp=as.POSIXct(paste(consumption_february$Date, consumption_february$Time)), "%d/%m/%Y %H:%M:%S")
#summary(consumption_february)
#Define space for 2 rows and 2 collumns of plots
par(mfrow=c(2,2))
# First plot
plot(consumption_february$timestamp, consumption_february$Global_active_power, type="l", xlab="", ylab="Global Active Power")
# Second plot
plot(consumption_february$timestamp, consumption_february$Voltage, type="l", xlab="datetime", ylab="Voltage")
# Third plot
plot(consumption_february$timestamp, consumption_february$Sub_metering_1, type="l", xlab="", ylab="Energy sub metering")
points(consumption_february$timestamp, consumption_february$Sub_metering_2, type="l", col="red")
points(consumption_february$timestamp, consumption_february$Sub_metering_3, type="l", col="blue")
legend("toprigh", col=c("black","red","blue"), legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), lty=c(1,1), lwd=c(1,1), bty="n", cex=.5)
# Forth plot
plot(consumption_february$timestamp, consumption_february$Global_reactive_power, type="l", xlab="datetime", ylab="Global_reactive_power")
# Save histogram to png file
dev.copy(png, file="plot4.png", width=480, height=480)
dev.off()