Last active
August 29, 2015 14:12
-
-
Save ilanman/e046b264eefd38b11b39 to your computer and use it in GitHub Desktop.
Bike Share blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(scales) | |
library(reshape) | |
## Get the data from here: https://archive.ics.uci.edu/ml/machine-learning-databases/00275/ | |
data <- read.csv('day.csv') | |
data$workingday <- as.factor(data$workingday) | |
data$weathersit <- as.factor(data$weathersit) | |
data$weekday <- as.factor(data$weekday) | |
data$mnth <- as.factor(data$mnth) | |
data$season <- as.factor(data$season) | |
data$holiday <- as.factor(data$holiday) | |
data$diff <- data$reg - data$cas | |
## Melt the data for ggplot. Could also use melt() from reshape package. I didn't | |
seasons <- split(data,data$season) | |
d1 = data.frame(dff1 = seasons[[1]]$diff,s=1) | |
d2 = data.frame(dff1 = seasons[[2]]$diff,s=2) | |
d3 = data.frame(dff1 = seasons[[3]]$diff,s=3) | |
d4 = data.frame(dff1 = seasons[[4]]$diff,s=4) | |
dall <- rbind(d1,d2,d3,d4) | |
## Loop through the seasons and save a .png of the density charts to later convert into a .gif | |
season = c("Summer","Spring","Fall","Winter") | |
cumulative_season <- as.character() | |
cumulative_count <- as.numeric() | |
for(i in seq_along(season)){ | |
cumulative_count <- c(cumulative_count,i) | |
cumulative_season <- append(cumulative_season,season[i]) | |
png(paste("plot",i,".png",sep='')) | |
p<-ggplot(dall[dall$s%in%cumulative_count,], | |
aes(fill = as.factor(s),x=as.numeric(dff1)))+ | |
geom_density(alpha=0.3,show_guide=T)+ | |
scale_y_continuous(limits=c(0,0.0005),labels=comma)+ | |
scale_x_continuous(limits=c(-500,7000)) + | |
xlab("Difference between Registered and Casual Riders")+ | |
ylab("Density of Days")+ | |
ggtitle("Distribution of the Difference\n of Registered - Casual riders by Season")+ | |
scale_fill_discrete(labels=cumulative_season, | |
name="Season") | |
print(p) | |
dev.off() | |
} | |
## Use ImageMagick to convert to .gif | |
system("convert -delay 80 *.png plot1.gif") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(scales) | |
library(reshape) | |
data <- read.csv('day.csv') | |
data$temps <- cut(data$atemp,breaks=seq(0,1.1,0.01)) | |
data$workingday <- as.factor(data$workingday) | |
data$weathersit <- as.factor(data$weathersit) | |
data$weekday <- as.factor(data$weekday) | |
data$mnth <- as.factor(data$mnth) | |
data$season <- as.factor(data$season) | |
data$holiday <- as.factor(data$holiday) | |
agg <- aggregate(data$cas,list(data$temps),sum) | |
names(agg) <- c("Group","cas") | |
agg$reg <- aggregate(data$reg,list(data$temps),sum)$x | |
## Calculate proportions to use in chart | |
agg$casprop <- agg$cas/sum(agg$cas) | |
agg$regprop <- agg$reg/sum(agg$reg) | |
plotdf <- agg[,c(1,4,5)] | |
## Melt data for ggplot | |
mdf <- melt(plotdf) | |
mdf$Group <- as.numeric(mdf$Group) | |
ggplot(mdf,aes(x=Group,y=value,color=variable))+ | |
geom_area(aes(fill=variable),stat='identity',position='fill',alpha=0.6,color='black')+ | |
scale_y_continuous(labels=percent,limits=c(0,1))+ | |
scale_x_continuous(limits=c(5,85),breaks=seq(5,85,10))+ | |
xlab("Temperature (Farenheit")+ | |
ylab("Proportion of Rider type")+ | |
ggtitle("Proportion of Rider Type using Bike Share\n as Temperature Increases")+ | |
scale_fill_discrete(name = "Rider Type",labels=c("Casual","Registered")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment