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
71 lines (56 loc) · 1.69 KB
/
plot4.R
File metadata and controls
71 lines (56 loc) · 1.69 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
read <- function()
{
fn <- "household_power_consumption.txt"
df <- read.csv(fn,
as.is = T,
dec = '.',
sep = ";",
na.strings = "?",
skip = 66636,
nrows = 2880,
col.names = c("Date","Time", "Global_AC", "Global_RC", "V", "Global_Int","Sub_1","Sub_2", "Sub_3"),
colClasses = c("character", "character", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"))
df$DateTime <- strptime(paste(df$Date, df$Time),"%d/%m/%Y %H:%M:%S")
df
}
createPlot4 <- function()
{
df <- read()
# needed this to switch to english day names in cyrilic r studio
Sys.setlocale("LC_TIME", "English")
par(mfrow = c(2,2))
plot(df$DateTime, df$Global_AC,
main = "",
ylab="Global Active Power (kilowatts)",
xlab="",
type="l"
)
plot(df$DateTime, df$V,
main = "",
ylab="Voltage",
xlab="datetime",
type="l"
)
plot(df$DateTime, df$Sub_1,
main = "",
ylab="Energy sub metering",
xlab="",
type="l"
)
lines(df$DateTime, df$Sub_2, col="Red")
lines(df$DateTime, df$Sub_3, col="Blue")
legend("topright", lty=1, cex=0.75,
c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"),
col=c("Black","Red","Blue"))
plot(df$DateTime, df$Global_RC,
main = "",
ylab="Global_reactive_power",
xlab="datetime",
type="l"
)
dev.copy(png,
res= 60, # had to use res to get rid of legend truncating.
# could be an issue of windows high-dpi display and so on
file="plot4.png")
dev.off()
}