diff --git a/Plot1.R b/Plot1.R new file mode 100644 index 00000000000..acb1775979b --- /dev/null +++ b/Plot1.R @@ -0,0 +1,21 @@ + +# Reading, naming and subsetting power consumption data +power <- read.table("household_power_consumption.txt",skip=1,sep=";") +names(power) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3") +subpower <- subset(power,power$Date=="1/2/2007" | power$Date =="2/2/2007") + +#PLOT 1 +# Transforming the Date and Time vars from characters into objects of type Date and POSIXlt respectively +subpower$Date <- as.Date(subpower$Date, format="%d/%m/%Y") +subpower$Time <- strptime(subpower$Time, format="%H:%M:%S") +subpower[1:1440,"Time"] <- format(subpower[1:1440,"Time"],"2007-02-01 %H:%M:%S") +subpower[1441:2880,"Time"] <- format(subpower[1441:2880,"Time"],"2007-02-02 %H:%M:%S") + +#calling the basic plot function +pdf(file = "plot1") +hist(as.numeric(as.character(subpower$Global_active_power)),col="blue",main="Global Active Power",xlab="Global Active Power(kilowatts)") + +# annotating graph +title(main="Global Active Power") +dev.off() +dev.copy(png, file = "plot1") \ No newline at end of file diff --git a/Plot1.png b/Plot1.png new file mode 100644 index 00000000000..e3fe2ab9167 Binary files /dev/null and b/Plot1.png differ diff --git a/Plot2.R b/Plot2.R new file mode 100644 index 00000000000..f6beb636edd --- /dev/null +++ b/Plot2.R @@ -0,0 +1,20 @@ + +#PLOT 2 +# Reading, naming and subsetting power consumption data +power <- read.table("household_power_consumption.txt",skip=1,sep=";") +names(power) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3") +subpower <- subset(power,power$Date=="1/2/2007" | power$Date =="2/2/2007") + +# Transforming the Date and Time vars from characters into objects of type Date and POSIXlt respectively +subpower$Date <- as.Date(subpower$Date, format="%d/%m/%Y") +subpower$Time <- strptime(subpower$Time, format="%H:%M:%S") +subpower[1:1440,"Time"] <- format(subpower[1:1440,"Time"],"2007-02-01 %H:%M:%S") +subpower[1441:2880,"Time"] <- format(subpower[1441:2880,"Time"],"2007-02-02 %H:%M:%S") +# calling the basic plot function +pdf(file = "plot2") +plot(subpower$Time,as.numeric(as.character(subpower$Global_active_power)),type="l",xlab="Time",ylab="Global Active Power (kilowatts)", col = "red") + +# annotating graph +title(main="Global Active Power over the week") +dev.off() +dev.copy(png, file = "plot2") diff --git a/Plot2.png b/Plot2.png new file mode 100644 index 00000000000..f2d34e90a47 Binary files /dev/null and b/Plot2.png differ diff --git a/Plot3.R b/Plot3.R new file mode 100644 index 00000000000..1a6d8d0c317 --- /dev/null +++ b/Plot3.R @@ -0,0 +1,26 @@ +#plot 3 + +# Reading, naming and subsetting power consumption data +power <- read.table("household_power_consumption.txt",skip=1,sep=";") +names(power) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3") +subpower <- subset(power,power$Date=="1/2/2007" | power$Date =="2/2/2007") + +# Transforming the Date and Time vars from characters into objects of type Date and POSIXlt respectively +subpower$Date <- as.Date(subpower$Date, format="%d/%m/%Y") +subpower$Time <- strptime(subpower$Time, format="%H:%M:%S") +subpower[1:1440,"Time"] <- format(subpower[1:1440,"Time"],"2007-02-01 %H:%M:%S") +subpower[1441:2880,"Time"] <- format(subpower[1441:2880,"Time"],"2007-02-02 %H:%M:%S") + + +# calling the basic plot functions +pdf(file = "plot3") +plot(subpower$Time,subpower$Sub_metering_1,type="n",xlab="Time",ylab="Energy sub metering") +with(subpower,lines(Time,as.numeric(as.character(Sub_metering_1)))) +with(subpower,lines(Time,as.numeric(as.character(Sub_metering_2)),col="green")) +with(subpower,lines(Time,as.numeric(as.character(Sub_metering_3)),col="blue")) +legend("topright", lty=1, col=c("black","green","blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3")) + +# annotating graph +title(main="Energy sub-metering") +dev.off() +dev.copy(png, file = "plot3") diff --git a/Plot3.png b/Plot3.png new file mode 100644 index 00000000000..185011159d5 Binary files /dev/null and b/Plot3.png differ diff --git a/Plot4.R b/Plot4.R new file mode 100644 index 00000000000..9fd781f317f --- /dev/null +++ b/Plot4.R @@ -0,0 +1,32 @@ +#plot4 +# Reading, naming and subsetting power consumption data +power <- read.table("household_power_consumption.txt",skip=1,sep=";") +names(power) <- c("Date","Time","Global_active_power","Global_reactive_power","Voltage","Global_intensity","Sub_metering_1","Sub_metering_2","Sub_metering_3") +subpower <- subset(power,power$Date=="1/2/2007" | power$Date =="2/2/2007") + +# Transforming the Date and Time vars from characters into objects of type Date and POSIXlt respectively +subpower$Date <- as.Date(subpower$Date, format="%d/%m/%Y") +subpower$Time <- strptime(subpower$Time, format="%H:%M:%S") +subpower[1:1440,"Time"] <- format(subpower[1:1440,"Time"],"2007-02-01 %H:%M:%S") +subpower[1441:2880,"Time"] <- format(subpower[1441:2880,"Time"],"2007-02-02 %H:%M:%S") + + +# initiating a composite plot with many graphs +par(mfrow=c(2,2)) + +# calling the basic plot function that calls different plot functions to build the 4 plots that form the graph +pdf(file = "plot4") +with(subpower,{ + plot(subpower$Time,as.numeric(as.character(subpower$Global_active_power)),type="l", xlab="Time",ylab="Global Active Power", col = "red") + plot(subpower$Time,as.numeric(as.character(subpower$Voltage)), type="l",xlab="Time",ylab="Voltage", col = "purple") + plot(subpower$Time,subpower$Sub_metering_1,type="n",xlab="Time",ylab="Energy sub metering") + with(subpower,lines(Time,as.numeric(as.character(Sub_metering_1)))) + with(subpower,lines(Time,as.numeric(as.character(Sub_metering_2)),col="green")) + with(subpower,lines(Time,as.numeric(as.character(Sub_metering_3)),col="blue")) + legend("topright", lty=1, col=c("black","green","blue"),legend=c("Sub_metering_1","Sub_metering_2","Sub_metering_3"), cex = 0.6) + plot(subpower$Time,as.numeric(as.character(subpower$Global_reactive_power)),type="l",xlab="Time",ylab="Global_reactive_power", col = "pink") +}) +dev.off() +dev.copy(png, file = "plot4") + + diff --git a/Plot4.png b/Plot4.png new file mode 100644 index 00000000000..8f4f3b3a0f1 Binary files /dev/null and b/Plot4.png differ