Coder Social home page Coder Social logo

rcode's Introduction

Rcode

plots knitr::opts_chunk$set(echo = TRUE, results = "asis") t <- read.table("household_power_consumption.txt", header=TRUE, sep=";", na.strings = "?", colClasses = c('character','character','numeric','numeric','numeric','numeric','numeric','numeric','numeric'))

Format date to Type Date

t$Date <- as.Date(t$Date, "%d/%m/%Y")

Filter data set from Feb. 1, 2007 to Feb. 2, 2007

t <- subset(t,Date >= as.Date("2007-2-1") & Date <= as.Date("2007-2-2"))

Remove incomplete observation

t <- t[complete.cases(t),]

Combine Date and Time column

dateTime <- paste(t$Date, t$Time)

Name the vector

dateTime <- setNames(dateTime, "DateTime")

Remove Date and Time column

t <- t[ ,!(names(t) %in% c("Date","Time"))]

Add DateTime column

t <- cbind(dateTime, t)

Format dateTime Column

t$dateTime <- as.POSIXct(dateTime)

Create the histogram

hist(t$Global_active_power, main="Global Active Power", xlab = "Global Active Power (kilowatts)", col="red")

Save file and close device

#dev.copy(png,"plot1.png", width=480, height=480) #dev.off()

Create Plot 2

plot(t$Global_active_power~t$dateTime, type="l", ylab="Global Active Power (kilowatts)", xlab="") #dev.copy(png,"plot2.png", width=480, height=480) #dev.off()

Create Plot 3

with(t, { plot(Sub_metering_1dateTime, type="l", ylab="Global Active Power (kilowatts)", xlab="") lines(Sub_metering_2dateTime,col='Red') lines(Sub_metering_3~dateTime,col='Blue') }) legend("topright", col=c("black", "red", "blue"), lwd=c(1,1,1), c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")) ## Saving to file #dev.copy(png, file="plot3.png", height=480, width=480) #dev.off()

Create Plot 4

par(mfrow=c(2,2), mar=c(4,4,2,1), oma=c(0,0,2,0)) with(t, { plot(Global_active_powerdateTime, type="l", ylab="Global Active Power (kilowatts)", xlab="") plot(VoltagedateTime, type="l", ylab="Voltage (volt)", xlab="") plot(Sub_metering_1dateTime, type="l", ylab="Global Active Power (kilowatts)", xlab="") lines(Sub_metering_2dateTime,col='Red') lines(Sub_metering_3dateTime,col='Blue') legend("topright", col=c("black", "red", "blue"), lty=1, lwd=2, bty="n", legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3")) plot(Global_reactive_powerdateTime, type="l", ylab="Global Rective Power (kilowatts)",xlab="") }) ## Saving to file #dev.copy(png, file="plot4.png", height=480, width=480) #dev.off()

rcode's People

Contributors

mandywillwin avatar

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.