2

A part of my code for ggplot is stored in a character vector. I would like to use this code as an additional geoms for my ggplot.

Example1:

  DF=data.frame(x=seq(1:10), y=seq(1:20))
   a='geom_line()'# This is a string that should be converted to RCode

So far I tried:

ggplot(DF, aes(x,y))+geom_point()+a
Error: Don't know how to add a to a plot

ggplot(DF, aes(x,y))+geom_point()+as.name(a)
Error: Don't know how to add as.name(a) to a plot

ggplot(DF, aes(x,y))+geom_point()+eval(parse(text=a))
Error in geom_line() + geom_line(y = 1) : 
non-numeric argument to binary operator

ggplot(DF, aes(x,y))+geom_point()+deparse(substitute(a))
Error: Don't know how to add deparse(substitute(a)) to a plot

Example 2:

DF=data.frame(x=seq(1:10), y=seq(1:20))
a='geom_line()+geom_line(y=1)'

Probable you are wondering, why I would like to do that in a first place? In a for loop, I created expressions and stored them in a list as characters. Later, I pasted together all expressions into a single string expression. Now, I would like to add this string to a ggplot command. Any suggestions?

Edit: Example 1 was successfully solved. But Example 2 stays unsolved.

2 Answers 2

3

the parse function has text argument you need to pass a to. Try:

ggplot(DF, aes(x,y)) + geom_point() + eval(parse(text = a))

More info here: http://adv-r.had.co.nz/Expressions.html#parsing-and-deparsing

In case of multiple statements, it is possible to deparse the original expression, add the new and then evaluate as a whole

original <- deparse(quote(ggplot(DF, aes(x,y)) + geom_point()))
new_call <- paste(original, '+', a)
eval(parse(text = new_call))
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, you are right, and I tried also with the text argument before, but for my original example did not work. When there are two geoms (e.g. a='geom_line()+geom_line(y=1)') in a string, eval(parse(text=a)) do not work. I will update my question with two geom arguments.
Don't edit the question directly please. Add it as an extra problem below the original one. The solution I put in is not actually solving the problem you now have :)
I think the problem is that the eval is trying to sum the parameters before adding it as an expression. A functional solution seems to deparse the original call, add the expression and then evaluate it as a whole.
0

You can also use a function to define these code into a list. Please see: https://homepage.divms.uiowa.edu/~luke/classes/STAT4580/dry.html

Here I cited the related code:

Defining a theme_slopegraph function to do the theme adjustment allows the adjustments to be easily reused:

theme_slopechart = function(toplabels = TRUE) {
     thm <- theme(...)
list(thm, ...) # add multiple codes
#...
}

p <- basic_barley_slopes ## from twonum.R
p + theme_slopechart()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.