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.
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()