This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot4.R
55 lines (35 loc) · 1.96 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#setting up directory
setwd("~/Dropbox/CourseraExploratory/project1")
#download file and loead dataset into R (power dataframe)
address <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(address, destfile="~/Dropbox/CourseraExploratory/project1/power.zip", method="curl")
unzip("~/Dropbox/CourseraExploratory/project1/power.zip")
power <- read.table("household_power_consumption.txt", sep=";", na.strings="?", header=TRUE)
#selecting data from 2007-02-01 to 2007-02-02 and load into sampel dataframe
library(lubridate)
power$Date <- dmy(power$Date)
power$Time <- hms(power$Time)
low_date <- ymd("2007-02-01")
up_date <- ymd("2007-02-02")
sample <- power[power$Date <= up_date & power$Date >= low_date, ]
rm(power)
#making plot 4
library(lubridate)
sample$moment <- paste(as.character(sample$Date), sample$Time)
sample$moment <- ymd_hms(sample$moment)
axis.size <- 1
label.size <- 1
png("plot4.png")
par(mfrow=c(2, 2), pty="m", mar=c(5, 4, 1, 0), oma=c(1, 1, 1, 1))
plot(sample$moment, sample$Global_active_power, type="n", xlab="", ylab="Global active power", cex.lab=label.size, cex.axis=0.7, mgp=c(2, 1, 0))
lines(sample$moment, sample$Global_active_power)
plot(sample$moment, sample$Voltage, type="n", xlab="datetime", ylab="Voltage", cex.lab=0.7, cex.axis=0.7, mgp=c(2, 1, 0))
lines(sample$moment, sample$Voltage)
plot(sample$moment, sample$Sub_metering_1, type="n", xlab="", ylab="Energy sub metering", cex.lab=0.7, cex.axis=0.7, mgp=c(2, 1, 0))
lines(sample$moment, sample$Sub_metering_1, col="grey")
lines(sample$moment, sample$Sub_metering_2, col="red")
lines(sample$moment, sample$Sub_metering_3, col="blue")
legend("topright", legend =colnames(sample[,c(7:9)]) , col=c("grey", "red", "blue"), lty=1, cex=0.4)
plot(sample$moment, sample$Global_reactive_power, type="n", xlab="datetime", ylab="Global_reactive_power", cex.lab=0.7, cex.axis=0.7, mgp=c(2, 1, 0))
lines(sample$moment, sample$Global_reactive_power)
dev.off()