|||

Using attr labels for ggplot

The problem

In social sciences, variables often have long and informative labels. Mostly they are too long to put into variable names, and often you want to keep short, systematic variable names as well as long labels. I am always messing about with changing the labels for my ggplot plots.

The solution

Here is a simple function to wrap a ggplot so that it uses the labels stored in attr(var,“label”) rather than the variable names for the x and y axis labels.

ggplotl=function(...){
  plot=ggplot(...)
  dat=plot$data
  for(m in names(plot$mapping)){
    char=paste0(plot$mapping[m])
    ml=attr(dat[,char],"label")
    plot$labels[m]=ml
  }

  plot
}

Example:

attr(mtcars$cyl,"label")="my label";ggplotl(data=mtcars,aes(x=mpg,y=cyl,colour=cyl))+geom_point()
Up next An evaluation puzzle: “Talent show” An NGO offers a play activity for Roma children, with the modest aim of “Providing safe and supportive spaces”. Most of the children don’t like the Examples of trivial graph format Can be imported by yED. The simplest version is this: etc. This version allows you to have longer node labels: (a
Latest posts Causal Map intro Causal Mapping - an earlier guide The walk to school in Sarajevo Glitches Draft blog post for AEA365 Theory Maker! Inventory & analysis of small conservation grants, C&W Africa - Powell & Mesbach! Lots of charts! Answering the “why” question: piecing together multiple pieces of causal information rbind.fill for 1-dimensional tables in r yED graph editor Examples of trivial graph format Using attr labels for ggplot An evaluation puzzle: “Talent show” An evaluation puzzle: “Mobile first” An evaluation puzzle: “Many hands” An evaluation puzzle: Loaves and fishes An evaluation puzzle: “Freak weather” An evaluation puzzle: “Billionaire” Publications Using Dropbox for syncing Shiny app data on Amazon EC2 Progress on the Causal Map app Articles and presentations related to Causal Maps and Theorymaker Better ways to present country-level data on a world map: equal-area cartograms A starter kit for reproducible research with R A reproducible workflow for evaluation reports Welcome to the Wiggle Room Realtime comments on a Theory of Change Responses to open questions shown as tooltips in a chart A panel on visualising Theories of Change for EES 2018? Peer mentoring for evaluators How do you explain reproducible research to clients?