I want to make a ggplot call variable to the user, without changing the ggplot call in the script...
I have a data frame with different columns I would love to use for label the dots on my plot. A lot of those columns are long and I want to provide a function 'shortname' to shorten the column a bit...
I also want to make this variable with a variable and this is where I stuck at the moment.
df <- data.frame(a=c('a;b;c','d;e;f'), b=c('A;B;C','D;E;F'),
x=c(1,2), y=c(2,3))
use_column <- 'b'
shortname <- function(x) {
sub('([^;]+).*', '\\1', x)
}
g <- ggplot(df, aes(x,y)) + geom_point()
g + geom_text(aes(label=a))
g + geom_text(aes(label=shortname(a)))
g + geom_text(aes(label=shortname(b)))
Up to this point, everything works as expected. The first plots shows column a shortened and the second plot shows column b. But when I try to use the variable use_column, I don't get it to run...
g + geom_text(aes_string(label=shortname(use_column)))
I am running out of ideas and hope somebody can help me on this.
Thanks in advance!