Giter VIP home page Giter VIP logo

exdata_plotting1's Introduction

Introduction

This assignment uses data from the UC Irvine Machine Learning Repository, a popular repository for machine learning datasets. In particular, we will be using the "Individual household electric power consumption Data Set" which I have made available on the course web site:

  • Dataset: Electric power consumption [20Mb]

  • 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:

  1. Date: Date in format dd/mm/yyyy
  2. Time: time in format hh:mm:ss
  3. Global_active_power: household global minute-averaged active power (in kilowatt)
  4. Global_reactive_power: household global minute-averaged reactive power (in kilowatt)
  5. Voltage: minute-averaged voltage (in volt)
  6. Global_intensity: household global minute-averaged current intensity (in ampere)
  7. 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).
  8. 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.
  9. 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.

Loading the data

When loading the dataset into R, please consider the following:

  • The dataset has 2,075,259 rows and 9 columns. First calculate a rough estimate of how much memory the dataset will require in memory before reading into R. Make sure your computer has enough memory (most modern computers should be fine).

  • We will only be using data from the dates 2007-02-01 and 2007-02-02. One alternative is to read the data from just those dates rather than reading in the entire dataset and subsetting to those dates.

  • You may find it useful to convert the Date and Time variables to Date/Time classes in R using the strptime() and as.Date() functions.

  • Note that in this dataset missing values are coded as ?.

Making Plots

Our overall goal here is simply to examine how household energy usage varies over a 2-day period in February, 2007. Your task is to reconstruct the following plots below, all of which were constructed using the base plotting system.

First you will need to fork and clone the following GitHub repository: https://github.com/rdpeng/ExData_Plotting1

For each plot you should

  • Construct the plot and save it to a PNG file with a width of 480 pixels and a height of 480 pixels.

  • Name each of the plot files as plot1.png, plot2.png, etc.

  • Create a separate R code file (plot1.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 should also include the code that creates the PNG file.

  • Add the PNG file and R code file to your git repository

When you are finished with the assignment, push your git repository to GitHub so that the GitHub version of your repository is up to date. There should be four PNG files and four R code files.

The four plots that you will need to construct are shown below.

Plot 1

plot of chunk unnamed-chunk-2

Plot 2

plot of chunk unnamed-chunk-3

Plot 3

plot of chunk unnamed-chunk-4

Plot 4

plot of chunk unnamed-chunk-5

exdata_plotting1's People

Contributors

rdpeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

exdata_plotting1's Issues

plot1.R

plot1 <- hist(Global_active_power, col ="red", main = paste("Global active power"), xlab = "Global active power (kilowatts)")
plot1 ## Create plot on screen device
dev.copy(png, file = "plot1.png") ## Copy plot to a PNG file
dev.off() ## close the PNG device

plot3.R

hpc <- read.csv("household_power_consumption.txt", sep=";",
na.strings="?",stringsAsFactors=FALSE)
hpc2<-subset(hpc,hpc$Date==c("1/2/2007","2/2/2007"))
hpc2$Timestamp = strptime(paste(hpc2$Date, hpc2$Time),
format = "%d/%m/%Y %H:%M:%S")
Sys.setlocale("LC_TIME", "C")
with(hpc2,plot(Timestamp,Sub_metering_1,type="l",col="black",
xlab=" ",ylab="Energy sub metering"))
points(hpc2$Timestamp,hpc2$Sub_metering_2,type="l",col="red")
points(hpc2$Timestamp,hpc2$Sub_metering_3,type="l",col="blue")
legend("topright",cex=0.6,lty=1:1,col=c("black","red","blue"),
legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
dev.copy(png,file="plot3.png")
dev.off()

plot2.R

hpc <- read.csv("household_power_consumption.txt", sep=";",
na.strings="?",stringsAsFactors=FALSE)
hpc2<-subset(hpc,hpc$Date==c("1/2/2007","2/2/2007"))
hpc2$Timestamp = strptime(paste(hpc2$Date, hpc2$Time),
format = "%d/%m/%Y %H:%M:%S")
Sys.setlocale("LC_TIME", "C")
with(hpc2,plot(Timestamp,Global_active_power, type ="l", ylab = "Global active power (kilowatts)"
,xlab=" ",ylim=c(0,6)))
dev.copy(png,file="plot2.png",width=480,height=480)
dev.off()

plots

plot1.R
plot1
data
+#plot 1
+PowerConsumption <- read.table("household_power_consumption.txt",sep=";",header=TRUE,stringsAsFactors=FALSE)
+head(PowerConsumption)
+dim(PowerConsumption)
+class(PowerConsumption)
+
+#Plot 1 - using data from the dates 02-01-2007 and 02-02-2007
+PowerConsumption$Date=strptime(PowerConsumption$Date, format="%d/%m/%Y")
+PowerConsumption$Global_active_power=as.numeric(PowerConsumption$Global_active_power)
+PowerConsumption$Global_intensity=as.numeric(PowerConsumption$Global_intensity)
+PowerConsumption$Global_reactive_power=as.numeric(PowerConsumption$Global_reactive_power)
+PowerConsumption$Sub_metering_1=as.numeric(PowerConsumption$Sub_metering_1)
+PowerConsumption$Sub_metering_2=as.numeric(PowerConsumption$Sub_metering_2)
+PowerConsumption$Sub_metering_3=as.numeric(PowerConsumption$Sub_metering_3)
+PowerConsumption$Voltage=as.numeric(PowerConsumption$Voltage)
+
+PowerConsumption=subset(PowerConsumption,PowerConsumption$Date>as.POSIXlt("2007-01-31") & PowerConsumption$Date<as.POSIXlt("2007-02-03"))
+
+png(file="plot1.png",width=480,height=480)
+hist(PowerConsumption$Global_active_power,col="red",main="Global Active Power",xlab="Global Active Power (kilowatts)")
+dev.off()

plot1.R

hpc <- read.csv("household_power_consumption.txt", sep=";", na.strings="?",
stringsAsFactors=FALSE)
hpc2 <- hpc[min(which(hpc$Date == "1/2/2007")):max(which(hpc$Date == "2/2/2007")),]
hist(hpc2$Global_active_power,main="Global Active Power",xlab="Global Active Power(kilowatts)",col="red",breaks=12,ylim=c(0,1200))
dev.copy(png,width=480,height=480,file="plot1.png")
dev.off()

possible minor issue in the 4th plot?

For the 4th plot:
4th_plot

The y-label of the 4th subplot (Global Reactive Power vs datetime) has not been formatted yet. The prettier y-label should be like, 'Global Reactive Power'.

Is this a bug or intention by the author?

plot4.R

hpc <- read.csv("household_power_consumption.txt", sep=";",
na.strings="?",stringsAsFactors=FALSE)
hpc2<-subset(hpc,hpc$Date==c("1/2/2007","2/2/2007"))
hpc2$Timestamp = strptime(paste(hpc2$Date, hpc2$Time),
format = "%d/%m/%Y %H:%M:%S")
Sys.setlocale("LC_TIME", "C")
par(mfrow=c(2,2))
with(hpc2,plot(Timestamp,Global_active_power,type="l",ylim=c(0,6),xlab=" ",ylab="Global Active Power"))
with(hpc2,plot(Timestamp,Voltage,type="l",ylim=c(234,246),xlab="datetime",ylab="Voltage"))
with(hpc2,plot(Timestamp,Sub_metering_1,type="l",col="black", xlab=" ",ylab="Energy sub metering"))
points(hpc2$Timestamp,hpc2$Sub_metering_2,type="l",col="red")
points(hpc2$Timestamp,hpc2$Sub_metering_3,type="l",col="blue")
legend("topright",cex=0.3,lty=1:1,bty="n",col=c("black","red","blue"), legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"))
with(hpc2,plot(Timestamp,Global_reactive_power,type="l",ylim=c(0.0,0.5),xlab="datetime",ylab="Global_reactive_power"))
dev.copy(png,file="plot4.png")
dev.off()

plot3.R

plot(Sub_metering_1, xlab=" ", ylab="Energy Sub Metering",
type="l", col="black")
lines(Sub_metering_2, col="red")
lines(Sub_metering_3, col="blue")
legend("topright", legend = c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), col = c("black", "red", "blue"), lty = c(1,1,1))
dev.copy(png, file = "plot3.png", width=480,height=480) ## Copy plot to a PNG file
dev.off() ## close the PNG device

plot2.R

plot2 <- plot(Global_active_power, type ="l", ylab = "Global active power (kilowatts)", xlab = "") ## Create plot on screen device

dev.copy(png, file = "plot2.png") ## Copy plot to a PNG file
dev.off() ## close the PNG device

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.