Skip to content

Commit

Permalink
course project 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ale210 committed Feb 7, 2015
1 parent 73fe5c6 commit 2dc2a66
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running

png(filename = "plot1.png")

lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]


t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)

t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")

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

dev.off()
Binary file added plot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running


png(filename = "plot2.png")

lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]


t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)

t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")

plot(
t$DateTime,
t$Global_active_power,
type="n",
ylab="Global Active Power (kilowatts)",
xlab="")

lines(t$DateTime, t$Global_active_power)

dev.off()
Binary file added plot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running

png(filename = "plot3.png")

lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]


t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)

t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")

plot(
t$DateTime,
t$Sub_metering_1,
type="n",
ylab="Energy sub metering",
xlab="")

lines(t$DateTime, t$Sub_metering_1, col="black")
lines(t$DateTime, t$Sub_metering_2, col="red")
lines(t$DateTime, t$Sub_metering_3, col="blue")

legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lwd = 1)

dev.off()
Binary file added plot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#the text file "household_power_consumption.txt" should
#be downloaded and installed in the working directory
#before running



loadData <- function(){
lines <- readLines("household_power_consumption.txt")
rows <- substr(lines, 0, 8) == "1/2/2007" | substr(lines, 0, 8) == "2/2/2007"
rows[1] = TRUE
data <- lines[rows]

t <- read.csv(
text = data,
na.strings = c("?"),
sep=";",
stringsAsFactors = FALSE)

t$DateTime <- strptime(paste(t$Date, t$Time), "%d/%m/%Y %H:%M:%S")
t
}

drawPlot1 <- function(tbl){

plot(
tbl$DateTime,
tbl$Global_active_power,
type="n",
ylab="Global Active Power",
xlab="")

lines(tbl$DateTime, tbl$Global_active_power)
}

drawPlot2 <- function(tbl){

plot(
tbl$DateTime,
tbl$Voltage,
type="n",
ylab="Voltage",
xlab="datetime")

lines(tbl$DateTime, tbl$Voltage)
}

drawPlot3 <- function(tbl){

plot(
tbl$DateTime,
tbl$Sub_metering_1,
type="n",
ylab="Energy sub metering",
xlab="")

lines(tbl$DateTime, tbl$Sub_metering_1, col="black")
lines(tbl$DateTime, tbl$Sub_metering_2, col="red")
lines(tbl$DateTime, tbl$Sub_metering_3, col="blue")

legend("topright", c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), col = c("black", "red", "blue"), lwd = 1)
}


drawPlot4 <- function(tbl){

plot(
tbl$DateTime,
tbl$Global_reactive_power,
type="n",
ylab="Global_reactive_power",
xlab="datetime")

lines(tbl$DateTime, tbl$Global_reactive_power)
}


t <- loadData()

png(filename = "plot4.png")

par(mfrow=c(2,2))

drawPlot1(t)
drawPlot2(t)
drawPlot3(t)
drawPlot4(t)

dev.off()








Binary file added plot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2dc2a66

Please sign in to comment.