forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
45 lines (32 loc) · 1.06 KB
/
plot2.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
#setting up the libraries
library(lubridate)
Sys.setlocale("LC_TIME", "en_US")
#Setting the wd and reading the file
setwd("~/Github/ExData_Plotting1")
#Reading my own file (also in github)
power <- read.csv("~/Github/ExData_Plotting1/household_power_consumption.txt", sep=";")
#Creating a new date-time column
datetime <- paste(power$Date, power$Time)
datetime
datetime <- dmy_hms(datetime)
power$datetime <- datetime
#Setting date and time to the right format
power$Date <- dmy(power$Date)
power$Time <- hms(power$Time)
str(power)
#subsetting the file
power <- subset(power,
power$Date == "2007-02-01" |
power$Date == "2007-02-02" )
#Setting the correct data types for the rest of the variables
names <- colnames(power[,c(3:9)])
power[,names] <- sapply(power[,names] , as.numeric)
#Creating png file
png("plot1.png",
#width = 480, height = 480)
?png
# Create plot2
plot(Global_active_power ~ datetime, data = power, col = "black",
type = "l", ylab = "Global Active Power (Kilowatts)", xlab = "")
# Close the device file
dev.off()