Skip to content

Instantly share code, notes, and snippets.

@psychemedia psychemedia/chart2txt.R
Last active Apr 29, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.