Skip to content

Commit

Permalink
add ploting script
Browse files Browse the repository at this point in the history
implements R code to generate plots,
adds plots to repo
  • Loading branch information
mbehzad authored Mar 2, 2017
1 parent 73fe5c6 commit 99cc04b
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
20 changes: 20 additions & 0 deletions plot1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# read data
data_ <- read.table("household_power_consumption.txt", sep = ";", na.strings = "?", header = T);
# Convert date and time
data_$Time <- strptime(x = paste(data_$Date, data_$Time, sep = " "), format = "%d/%m/%Y %H:%M:%S")
data_$Date <- as.Date(data_$Date,format = '%d/%m/%Y')

start_ <- as.Date("2007-02-01")
end_ <- as.Date("2007-02-02")

data2 <- data_[which(data_$Date >= start_ & data_$Date <= end_),]

# draw plot 1
png(file = "plot1.png", width = 480, height = 480, units = "px")

hist(data2$Global_active_power,
main="Global Active Power",
xlab="Global Active Power (kilowatts)",
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.
23 changes: 23 additions & 0 deletions plot2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# read data
data_ <- read.table("household_power_consumption.txt", sep = ";", na.strings = "?", header = T);
# Convert date and time
data_$Time <- strptime(x = paste(data_$Date, data_$Time, sep = " "), format = "%d/%m/%Y %H:%M:%S")
data_$Date <- as.Date(data_$Date,format = '%d/%m/%Y')

start_ <- as.Date("2007-02-01")
end_ <- as.Date("2007-02-02")

data2 <- data_[which(data_$Date >= start_ & data_$Date <= end_),]

# draw plot 2
png(file = "plot2.png", width = 480, height = 480, units = "px")

plot(
x = data2$Time,
y = data2$Global_active_power,
type = "l",
xlab="",
ylab="Global Active Power (kilowatts)"
)

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.
35 changes: 35 additions & 0 deletions plot3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# read data
data_ <- read.table("household_power_consumption.txt", sep = ";", na.strings = "?", header = T);
# Convert date and time
data_$Time <- strptime(x = paste(data_$Date, data_$Time, sep = " "), format = "%d/%m/%Y %H:%M:%S")
data_$Date <- as.Date(data_$Date,format = '%d/%m/%Y')

start_ <- as.Date("2007-02-01")
end_ <- as.Date("2007-02-02")

data2 <- data_[which(data_$Date >= start_ & data_$Date <= end_),]

# draw plot 3
png(file = "plot3.png", width = 480, height = 480, units = "px")

plot(
x = data2$Time,
y = data2$Sub_metering_1,
type = "l",
xlab="",
ylab="Energy sub metering"
)

lines(
x=data2$Time,
y=data2$Sub_metering_2,
col="red")

lines(
x=data2$Time,
y=data2$Sub_metering_3,
col="blue")

legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),bty="l",col=c("black","red","blue"),lwd=2,cex=0.7)

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.
64 changes: 64 additions & 0 deletions plot4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# read data
data_ <- read.table("household_power_consumption.txt", sep = ";", na.strings = "?", header = T);
# Convert date and time
data_$Time <- strptime(x = paste(data_$Date, data_$Time, sep = " "), format = "%d/%m/%Y %H:%M:%S")
data_$Date <- as.Date(data_$Date,format = '%d/%m/%Y')

start_ <- as.Date("2007-02-01")
end_ <- as.Date("2007-02-02")

data2 <- data_[which(data_$Date >= start_ & data_$Date <= end_),]

# draw plot 4
png(file = "plot4.png", width = 480, height = 480, units = "px")

par(mfrow=c(2,2))
# 1
plot(
x = data2$Time,
y = data2$Global_active_power,
type = "l",
xlab="",
ylab="Global Active Power"
)

# 2
plot(
x = data2$Time,
y = data2$Voltage,
type = "l",
xlab="datetime",
ylab="Voltage"
)

# 3
plot(
x = data2$Time,
y = data2$Sub_metering_1,
type = "l",
xlab="",
ylab="Energy sub metering"
)

lines(
x=data2$Time,
y=data2$Sub_metering_2,
col="red")

lines(
x=data2$Time,
y=data2$Sub_metering_3,
col="blue")

legend("topright",c("Sub_metering_1","Sub_metering_2","Sub_metering_3"),bty="l",col=c("black","red","blue"),lwd=2,cex=0.7)

# 4
plot(
x = data2$Time,
y = data2$Global_reactive_power,
type = "l",
xlab="datetime",
ylab="Global_reactive_power"
)

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 99cc04b

Please sign in to comment.