forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot4.R
69 lines (61 loc) · 1.36 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Load sqldf to enable partial read of CSV
library(sqldf)
# Load tidyverse to work with tibbles
library(tidyverse)
# Load only records for 2007-02-01 and 2007-02-02
data <- read.csv.sql(
"household_power_consumption.txt",
sql = "select * from file where Date in ('1/2/2007', '2/2/2007')",
sep = ";"
)
# Convert to tibble
data <- as_tibble(data)
# Convert Date and Time columns into a datetime
data <- data %>%
unite("Date.Time", Date:Time) %>%
mutate(Date.Time = as.POSIXct(Date.Time, format = "%d/%m/%Y_%H:%M:%S"))
png(filename = "plot4.png", width = 480, height = 480)
par(mfrow = c(2, 2))
plot(
data$Date.Time,
data$Global_active_power,
type = "l",
xlab = "",
ylab = "Global Active Power"
)
plot(
data$Date.Time,
data$Voltage,
type = "l",
xlab = "datetime",
ylab = "Voltage"
)
plot(
data$Date.Time,
data$Sub_metering_1,
type = "l",
xlab = "",
ylab = "Energy sub metering"
)
with(
data, {
lines(Date.Time, Sub_metering_2, col = "red")
lines(Date.Time, Sub_metering_3, col = "blue")
}
)
legend(
"topright",
names(data[, 6:8]),
col = c("black", "red", "blue"),
lty = 1,
bty = "n"
)
plot(
data$Date.Time,
data$Global_reactive_power,
type = "l",
xlab = "datetime",
ylab = "Global_reactive_power"
)
axis(2, at = seq(0.1, 0.5, 0.1))
dev.off()