Skip to content

Instantly share code, notes, and snippets.

@psychemedia
Last active April 29, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psychemedia/1ee9214becf289eb3e0818f5fe7f0a58 to your computer and use it in GitHub Desktop.
Save psychemedia/1ee9214becf289eb3e0818f5fe7f0a58 to your computer and use it in GitHub Desktop.
Quick test - textualising ggplot charts
library(ggplot2)
g=ggplot(economics_long, aes(date, value01, colour = variable)) + geom_line()+ggtitle('dummy title')
#The label values may not be the limts
txt=paste('The chart titled"', g$labels$title,'"',
'with x-axis', g$labels$x,'labeled from',ggplot_build(g)$panel$ranges[[1]]$x.labels[1],'to',tail(ggplot_build(g)$panel$ranges[[1]]$x.labels,n=1),
'and y-axis', g$labels$y,'labeled from',ggplot_build(g)$panel$ranges[[1]]$y.labels[1],'to',tail(ggplot_build(g)$panel$ranges[[1]]$y.labels,n=1),sep=' ')
if ('colour' %in% attributes(g$labels)$names){
txt=paste(txt,'\nColour is used to represent',g$labels$colour)
if ( class(g$data[[g$labels$colour]]) =='factor') {
txt=paste(txt,', a factor with levels: ',
paste(levels(g$data[[g$labels$colour]]), collapse=', '), '.', sep='')
}
}
txt
#"The chart titled "dummy title" with x-axis date labeled from 1970 to 2010 and y-axis value01 labeled from 0.00 to 1.00
#Colour is used to represent variable, a factor with levels: pce, pop, psavert, uempmed, unemploy."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment