Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Course Project 1 #967

Open
YS707070 opened this issue Jan 5, 2021 · 0 comments
Open

Course Project 1 #967

YS707070 opened this issue Jan 5, 2021 · 0 comments

Comments

@YS707070
Copy link

YS707070 commented Jan 5, 2021

data <- read.csv("D:\User\Courses\R Programming\Week1//household_power_consumption.txt", sep=";", na.strings="?",stringsAsFactors = FALSE)
df<- subset(data, data$Date=="1/2/2007"|data$Date=="2/2/2007")
df$Global_active_power <- as.numeric(as.character(df$Global_active_power))
df$Global_reactive_power <- as.numeric(as.character(df$Global_reactive_power))
df$Voltage <- as.numeric(as.character(df$Voltage))
df$Sub_metering_1 <- as.numeric(as.character(df$Sub_metering_1))
df$Sub_metering_2 <- as.numeric(as.character(df$Sub_metering_2))
df$Sub_metering_3 <- as.numeric(as.character(df$Sub_metering_3))
df$Datetime <-paste(df$Date, df$Time)

#Plot1
png("plot1.png", width=480, height=480)
hist(df$Global_active_power, col="red", main="Global Active Power",xlab="Global Active Power (kilowatts)")
dev.off()
plot1

#Plot2
png("plot2.png", width=480, height=480)
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Global_active_power, type="l", xlab="",ylab="Global Active Power(Kilowatts)" )
dev.off()
plot2

#Plot3
png("plot3.png", width=480, height=480)
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering")
lines(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_2, type = "l", col="red")
lines(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_3, type = "l", col="blue")
legend("topright",lty=1, col=c("black", "red", "blue"), legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
plot3

#Plot4
png("plot4.png", width=480, height=480)
par(mfcol = c(2,2))
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Global_active_power, type="l", xlab="",ylab="Global Active Power(Kilowatts)" )
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_1, type = "l", xlab = "", ylab = "Energy sub metering")
lines(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_2, type = "l", col="red")
lines(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Sub_metering_3, type = "l", col="blue")
legend("topright",lty=1, col=c("black", "red", "blue"), legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"))
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Voltage, type = "l", xlab = "datetime", ylab = "Voltage")
plot(strptime(df$Datetime, "%d/%m/%Y %H:%M:%S"), df$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power")
dev.off()
plot4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant