Last active
August 29, 2015 14:04
-
-
Save cincodenada/c1fe2664a17cab2903fd to your computer and use it in GitHub Desktop.
Hotel Graph from /r/dataisugly
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(grid) | |
nicer_still = T | |
hotelstuff = read.csv('hotelstuff.tsv',sep="\t") | |
if(nicer_still) { | |
#Order by revenue change | |
hotelstuff = hotelstuff[with(hotelstuff,order(-Revenue.Change)),] | |
#Take out the Total Revenue row and keep it for the hline | |
totalrow = hotelstuff[hotelstuff$Category=="Total Department Revenue",] | |
hotelstuff <- hotelstuff[hotelstuff$Category!="Total Department Revenue",] | |
} | |
# Cement the row order, for easier comparison to original chart | |
hotelstuff$Category <- factor(hotelstuff$Category,levels=rev(hotelstuff$Category),ordered=TRUE) | |
# Open the image | |
png('hotelstuff.png',width=1000,height=500) | |
p = ggplot(hotelstuff,aes(x=Category,y=Revenue.Change,fill=sign(Revenue.Change))) + | |
geom_bar(stat='identity') + | |
coord_flip() + | |
# Set title and y-label | |
ggtitle(expression( | |
atop("Total F&B Department Revenues", | |
atop("Full-Service Hotels, Change 2007-2012") | |
) | |
)) + | |
ylab("Revenue Change") + | |
# Set the red/black colors - since fill is sign() a gradient is fine, since values will be either 1 or -1 | |
scale_fill_gradient2(low="red",high="black",guide="none") + | |
# Add the bar labels | |
geom_text(aes( | |
# Generate the percent labels | |
label=paste(format(hotelstuff$Revenue.Change*100,3),"%",sep=""), | |
# hjust has the nifty property of actually being how much to shift the label, | |
# in units of the label length, so we can start with 1/0 to get it outside the bar | |
# then add/subtract a little to add some margin | |
hjust=(Revenue.Change < 0) - sign(Revenue.Change)*0.1 | |
), size=5) + | |
# Add a 0-axis | |
geom_hline(yintercept=0) + | |
#Expand the scale a bit to make room for bar labels, and label it in percentages | |
scale_y_continuous(expand=c(0,0.045),labels=percent_format()) + | |
#Up the base text size | |
theme( | |
text=element_text(size=20), | |
axis.title=element_text(vjust=-1.1), | |
plot.margin=unit(c(1,1,3,0.5),"lines"), | |
axis.title.y=element_blank() | |
) | |
if(nicer_still) { | |
#Add the total line | |
p = p + geom_hline( | |
yintercept=totalrow$Revenue.Change, | |
alpha=0.5, | |
linetype='longdash' | |
) + | |
#And label it | |
annotate("text", | |
x=max(hotelstuff$Category), | |
y=totalrow$Revenue.Change, | |
hjust=1.03, | |
vjust=-0.5, | |
alpha=0.5, | |
label=paste0( | |
"Department Total Change: ",format(totalrow$Revenue.Change*100,3),"%" | |
), | |
) | |
} | |
# Render the plot | |
p | |
# Add footer text | |
# adapted from http://www.r-bloggers.com/r-good-practice-%E2%80%93-adding-footnotes-to-graphics/ | |
# via http://stackoverflow.com/questions/3039438/ggplot2-footnote | |
# I had issues with expression() and newlines, hence the two grid.text() calls | |
grid.text(label=expression( | |
# use expression() to get italics inline | |
"Sample: 667 Full-Service Hotels - Average: 382 Rooms - Reported Data 2007 through 2012." ~ | |
"Source: PKF Hospitality Research, LLC, Anuual" ~ italic("Trends® in the Hotel Industry") ~ "Report" | |
), | |
x=unit(2,"mm"), | |
y=unit(2,"mm"), | |
just=c("left","bottom"), | |
gp=gpar(cex=1,col=grey(.5),lheight=0.5) | |
) | |
dev.off() |
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
Category | Revenue Change | |
---|---|---|
Restaurant | -.265 | |
Bar/Lounge | .049 | |
Room Service | -.095 | |
Mini Bar | -.28 | |
Banquet | -.02 | |
Public Room Rental | -.248 | |
A/V Rental | .146 | |
Service Charges | .055 | |
Other F&B Income | -.274 | |
Total Department Revenue | -.105 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment