Skip to content

Instantly share code, notes, and snippets.

@dggoldst
Last active August 29, 2015 14:06
Show Gist options
  • Save dggoldst/4e55ef8a07a694521cc9 to your computer and use it in GitHub Desktop.
Save dggoldst/4e55ef8a07a694521cc9 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(dplyr)
library(reshape2)
df=structure(list(Year = c(2013L, 2012L, 2011L, 2010L, 2009L, 2008L,
2007L, 2006L, 2005L, 2004L, 2003L, 2002L, 2001L, 2000L, 1999L,
1998L, 1997L),
City = structure(c(14L, 8L, 12L, 13L, 1L, 2L,
6L, 4L, 14L, 8L, 15L, 5L, 10L, 9L, 7L, 3L, 11L),
.Label = c("Boston", "Chicago", "Dallas", "Houston", "Kansas City", "Long Beach",
"Los Angeles", "Minneapolis", "New Orleans", "Orlando", "Philadelphia",
"Seattle", "St. Louis", "Toronto", "Vancouver"), class = "factor"),
Zone = structure(c(2L, 1L, 3L, 1L, 2L, 1L, 3L, 1L, 2L, 1L,
3L, 1L, 2L, 1L, 3L, 1L, 2L),
.Label = c("Central", "East", "West"), class = "factor"),
Attendance = c(537L, 416L, 477L,
475L, 473L, 474L, 409L, 358L, 430L, 370L, 334L, 230L, 254L,
258L, 249L, 216L, 235L), Membership = c(1435L, 1471L, 1727L,
1464L, 1331L, 1175L, 1130L, 788L, 888L, 884L, 839L, 907L,
871L, 874L, 879L, 877L, 849L), Tracks = c(3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L)),
.Names = c("Year", "City", "Zone", "Attendance", "Membership", "Tracks"),
class = "data.frame",
row.names = c(NA, -17L))
df=df %>%
mutate(
Proportion_Attending=Attendance/Membership,
SEAttending=sqrt(Proportion_Attending*(1-Proportion_Attending)/Membership),
Attendees_Per_Track = Attendance / Tracks,
YearCity = paste(Year,City)) %>%
select(
-Tracks
)
df$Zone=factor(df$Zone,levels=c("East","Central","West"))
dfl=melt(df,id=c("YearCity","City","Zone","Year"))
p=ggplot(filter(dfl,!(variable %in% c("SEAttending"))),aes(x=YearCity,y=value,group=variable))
p=p+geom_line()
p=p+theme( axis.text.x = element_text(angle=90, vjust=0.5))
p=p+facet_grid(variable~., scales="free_y")
p=p+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
p
ggsave(plot=p,file="multi.png",width=4,height=8)
p=ggplot(df,aes(x=YearCity,y=Proportion_Attending,group=1,color=Zone))
p=p+geom_point()
p=p+geom_errorbar(width=.1,aes(ymin=Proportion_Attending-SEAttending,ymax=Proportion_Attending+SEAttending))
p=p+ylab("Attendance Rate")
p=p+theme( axis.text.x = element_text(angle=90, vjust=0.5),legend.position="bottom",axis.title.x=element_blank())
p
ggsave(plot=p,file="cities.c.png",width=4,height=6)
dfs = df %>%
group_by(Zone) %>%
summarize(
going=sum(Attendance),
not_going=sum(Membership)-sum(Attendance),
mu=going/(going+not_going),
se=sqrt((mu*(1-mu))/(going+not_going))
)
dfs$Zone=factor(dfs$Zone,levels=c("East","Central","West"))
p=ggplot(dfs,aes(x=Zone,y=mu,color=Zone))
p=p+geom_point(size=3)
p=p+geom_errorbar(width=.1,aes(ymin=mu-se,ymax=mu+se))
p=p+ylab("Attendance Rate")
p=p+theme( axis.text.x = element_text(angle=90, vjust=0.5),legend.position="none")
p
ggsave(plot=p,file="attendance.c.png",width=4,height=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment