Exploratory data analysis

Course Project 1.

In [1]:
%load_ext rmagic
In [10]:
%%R
d = read.table('household_power_consumption.txt', header=TRUE, sep=';', na.strings = "?", 
               colClasses=c('character', 'character', 'numeric', 'numeric', 'numeric', 
                            'numeric', 'numeric', 'numeric', 'numeric'))

# select only two days 2007-02-01 and 2007-02-02
d = d[(d$Date == '1/2/2007') | (d$Date == '2/2/2007'),]

# convert date
d$Date = as.Date(d$Date, "%d/%m/%Y")
d$DateTime = as.POSIXct(paste(d$Date, d$Time), format="%Y-%m-%d %H:%M:%S")
head(d)
            Date     Time Global_active_power Global_reactive_power Voltage
66637 2007-02-01 00:00:00               0.326                 0.128  243.15
66638 2007-02-01 00:01:00               0.326                 0.130  243.32
66639 2007-02-01 00:02:00               0.324                 0.132  243.51
66640 2007-02-01 00:03:00               0.324                 0.134  243.90
66641 2007-02-01 00:04:00               0.322                 0.130  243.16
66642 2007-02-01 00:05:00               0.320                 0.126  242.29
      Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
66637              1.4              0              0              0
66638              1.4              0              0              0
66639              1.4              0              0              0
66640              1.4              0              0              0
66641              1.4              0              0              0
66642              1.4              0              0              0
                 DateTime
66637 2007-02-01 00:00:00
66638 2007-02-01 00:01:00
66639 2007-02-01 00:02:00
66640 2007-02-01 00:03:00
66641 2007-02-01 00:04:00
66642 2007-02-01 00:05:00

Plot 1

In [12]:
%%R
hist(d$Global_active_power, xlab='Global Active Power (kilowatts)', main='Global Active Power', col='red')

Plot 2

In [14]:
%%R
with(d, 
     plot(DateTime, Global_active_power, type='l', ylab='Global Active Power (kilowatts)', xlab=''))

Plot 3

In [16]:
%%R
with(d, {
    plot(DateTime, Sub_metering_1, type='l', col='black', ylab='Energy sub metering', xlab='', ylim=c(0,38))
    lines(DateTime, Sub_metering_2, type='l', col='red')
    lines(DateTime, Sub_metering_3, type='l', col='blue')
    legend('topright', c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), lty=1, col=c('black', 'red', 'blue'))
    }
)

Plot 4

In [17]:
%%R
par(mfrow=c(2,2))
with(d,{
    plot(DateTime, Global_active_power, type='l', ylab='Global Active Power', xlab='')
    plot(DateTime, Voltage, type='l', xlab='datetime')
    
    plot(DateTime, Sub_metering_1, type='l', col='black', ylab='Energy sub metering', xlab='')
    lines(DateTime, Sub_metering_2, type='l', col='red')
    lines(DateTime, Sub_metering_3, type='l', col='blue')
    legend('topright', c('Sub_metering_1', 'Sub_metering_2', 'Sub_metering_3'), lty=1, col=c('black', 'red', 'blue'), bty='n')
    
    plot(DateTime, Global_reactive_power, type='l', xlab='datetime')
})