r functions: city skylines

Summary

An artistic rendering of Boston's skyline using R. Code and source data are included.

boston city skyline in polar coordinates

Been interested in using R (or other programming languages) to make artistic renderings of data—that is, representations that might not be as informative as well-made graphs, but are pleasing to look at purely for enjoyment. Took a stab at it earlier this year when representing usage statistics of a music group (club coat check member statistics) and for Cheyne-Stokes respiration. Wanted to make an artistic rendering of Boston's city skyline, again leaning on the use of polar coordinates as it is visually more interesting that using Cartesian. The code and csv file are included below.

boston building height csv files

download city_skylines_boston.R

R / S+
  1. # makes an artistic rendering of the boston city skyline
  2. # biafra ahanonu
  3. # updated: 2013.12.28
  4.  
  5. main <-function(inputFile,savePlot=1,saveFilename="city_skyline_boston.png",nrow=2,ncol=5,...){
  6.         # load in the data
  7.         dataFile = read.table(inputFile,sep=",",header=T);
  8.         # randomly group the buildings
  9.         dataFile$cat = round(runif(nrow(dataFile),1,nrow))
  10.         dataFile$scat = round(runif(nrow(dataFile),1,ncol))
  11.         # plot the data
  12.         cityPlot = ggplot(dataFile,aes(x=sample(c(nrow(dataFile))),y=stories,fill=building))+
  13.         scale_fill_hue()+
  14.         guides(fill=FALSE,group=FALSE)+
  15.         geom_bar(stat="identity",position="dodge")+
  16.         theme(
  17.                 axis.text.x = element_text(angle=90,hjust=1,vjust=.5,color="white"),
  18.                 panel.background=element_rect(fill="white",colour="white"),
  19.                 strip.text = element_blank(),
  20.                 strip.background = element_blank(),
  21.                 panel.grid = element_blank(),
  22.                 axis.text = element_blank(),
  23.                 axis.ticks = element_blank(),
  24.                 axis.title = element_blank()
  25.                 )+
  26.         facet_grid(cat~scat)+
  27.         coord_polar()
  28.  
  29.         # decide whether to save or display plot
  30.         if(savePlot==1){
  31.                 png(paste(saveFilename,sep=""),width=2400,height=2400,res=400,pointsize=1,antialias = "cleartype")
  32.                         print(cityPlot)
  33.                         # makeFootnote()
  34.                 dev.off()
  35.         }else{
  36.                 print(cityPlot)
  37.         }
  38. }
  39.  
  40. # run script
  41. main("buildings.height.boston.csv");

-biafra
bahanonu [at] alum.mit.edu

additional articles to journey through:

r functions: knitr wrapper
13 july 2013 | programming

Thought i'd share the wrapper function i use to knit files using the excellent knitr[...] package in R. This allows easy creation of an html page that integrates commentary, R code, and the resulting plots (which are base64 encoded, so the html file is fully portable).

¿qué es la calle?
24 may 2013 | short story | spanish

Había terminado y salé para mi cocina. Tenía hambre pero este día no había comida dentro de mi despensa. Me fui y caminé hacienda[...] la Tport—una máquina que puede transportar una persona a otro lugar sin energía y tiempo. Cuando entré la máquina, toqué algunos botónes y esperé. Pero nada ocurrió y lo hice las mismas acciones otra vez—y nada ocurrió.

How would the disappearance of streets affect the social fabric? This short story briefly (in castellano!) explores a world in which instantaneous, free transport is possible. Meant mainly to practice my spanish, i plan to follow-up with a more detail story in the future.

archive everything!
01 october 2012 | notes

Archived everything in my inbox. It's awesome. Continues my general trend of simplifying. Logging off websites, only checking the news for [...]brief periods, and focusing on a core set of hobbies. Eliminating distractions and reducing information overload are doing wonders to fight off stress and keep me humming along.

bio42: diagrams, part 1
25 january 2013 | teaching

Had a couple minutes to spare before leaving lab, so decided to throw together some diagrams to help explain a couple biological pathways s[...]tudents of bio42, a bio class at Stanford I'm TAing. Hoping to make a set for each system we study. Started with vesicle budding and fusion along with muscle contraction in smooth and skeletal muscles.

©2006-2024 | Site created & coded by Biafra Ahanonu | Updated 21 October 2024
biafra ahanonu