Measurements of Electric Power Consumption

Measurements of Electric Power Consumption

Instructions 

Description: Measurements of electric power consumption in one household with a one-minute sampling rate over a period of almost 4 years. Different electrical quantities and some sub-metering values are available.

The following descriptions of the 9 variables in the dataset are taken from the UCI web site:

  • Date: Date in format dd/mm/yyyy
  • Time: time in format hh:mm:ss
  • Global_active_power: household global minute-averaged active power (in kilowatt)
  • Global_reactive_power: household global minute-averaged reactive power (in kilowatt)
  • Voltage: minute-averaged voltage (in volt)
  • Global_intensity: household global minute-averaged current intensity (in ampere)
  • Sub_metering_1: energy sub-metering No. 1 (in watt-hour of active energy). It corresponds to the kitchen, containing mainly a dishwasher, an oven and a microwave (hot plates are not electric but gas powered).
  • Sub_metering_2: energy sub-metering No. 2 (in watt-hour of active energy). It corresponds to the laundry room, containing a washing-machine, a tumble-drier, a refrigerator and a light.
  • Sub_metering_3: energy sub-metering No. 3 (in watt-hour of active energy). It corresponds to an electric water-heater and an air-conditioner.
Making Plots
Load the dataset into R. Your task is to reconstruct the following plots below, all of which were constructed using the base plotting system.

For each plot, you should:

  • Construct the plot and save it to a PNG file.
  • Name each of the plot files as png, plot2.png, etc.
  • Create a separate R code file (R, plot2.R, etc.) that constructs the corresponding plot, i.e. code in plot1.R constructs the plot1.png plot. Your code file should include code for reading the data so that the plot can be fully reproduced. You must also include the code that creates the PNG file.
  • Add the PNG files and R code files to a zip file and submit it online on Moodle.

Plot 1 [1 Mark]

Plot 2 [1 Mark]

Plot 3 [1.5 Marks]

Plot 4 [1.5 Marks] 

Solution 

plot1.R

png(“Rplot1.png”)

x<-read.table(“Dataset-3.txt”,header = T,sep=”;”) #first change the working directory according to the file location

hist(x$Global_active_power[x$Voltage==237],col=”red”,xlab = “Global Active Power(kilowatts) for Voltage 237”, main = “Global Active Power”) #histogram with proper x labelling and naming(main)

dev.off() 

plot2.R 

x<-read.table(“Dataset-3.txt”,header = T,sep=”;”)#data loading

png(“Rplot2.png”)

boxplot(x$Global_active_power[x$Voltage==237],xlab=”Voltage 237″,main=”Global Active Power(kilowatts) for Voltage 237″,col = “red”) #histogram with proper x labelling ,color and naming(main)

dev.off() #save the file

 plot3.R 

x<-read.table(“Dataset-3.txt”,header = T,sep=”;”)

png(“Rplot3.png”)

z <-x$Global_active_power[x$Voltage>=249]

y <-x$Voltage[x$Voltage>=249]

plot(y,z, xlab=”Voltage”,ylab=”Global_Active_power” ,main=”Global Active Power (kilowatts) for Voltage >= 249″,col=ifelse(y<250,”black”,”red”))# color according to voltage value , xlab,ylab ,main acc. to labels given

legend(“topright”,c(“Voltage >=249 &<250″,”Voltage >= 250”),col=c(“black”,”red”),pch=1) #pch gives shape of points , col gives colors , topright gives location of legend

dev.off() 

plot4.R 

x<-read.table(“Dataset-3.txt”,header = T,sep=”;”)#data loading

png(“Rplot4.png”)

par(mfrow=c(3,1)) #to create 3 histograms in the same plot

hist(x$Global_active_power[x$Sub_metering_2==0],xlab = “Global Active Power(kilowatts)”,col=”red”,main=”Global Active Power for Sub_metering_2=0.000″)

hist(x$Global_active_power[x$Sub_metering_2==1],xlab = “Global Active Power(kilowatts)”,col=”red”,main=”Global Active Power for Sub_metering_2=1.000″)

hist(x$Global_active_power[x$Sub_metering_2==2],xlab = “Global Active Power(kilowatts)”,col=”red”,main=”Global Active Power for Sub_metering_2=2.000″)

dev.off()#save file