forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot2.R
23 lines (19 loc) · 1.05 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
#reads data from household_power_consumption.txt file between dates 2007-02-01 and 2007-02-02
#converts date and time fields to date and time objects
#returns the dataframe with the column names col_names defined in the function
read_data <- function(){
col_names <- c("Date", "Time", "Global_active_power", "Global_reactive_power", "Voltage", "Global_intenstiy", "Sub_metering_1", "Sub_metering_2", "Sub_metering_3")
data <- read.table("household_power_consumption.txt", header = FALSE, sep = ";", skip = 66638, nrows=2880, na.strings=c("?"), col.names = col_names)
dt <- paste(data$Date, data$Time, sep = " ")
t <- lapply(dt, function(d) strptime(d, "%d/%m/%Y %T"))
data$Time <- t
dates <- lapply(data$Date, function(x){as.Date(x, format = "%d/%m/%Y")})
data$Date <- dates
return(data)
}
d <- read_data()
plot.new()
png(filename="plot2.png", width=480, height=480)
plot(d$Global_active_power, type = "l", ylab = "Global Active Power (kilowatts)", xlab="", xaxt="n")
axis(side=1, labels= c("Thu", "Fri", "Sat"), at = c(0, 1500, 2900))
dev.off()