baptiste / gridExtra

Miscellaneous Functions for "Grid" Graphics
http://cran.r-project.org/web/packages/gridExtra/index.html
15 stars 4 forks source link

grid.arrange(..., top = xyz) should accept expressions #43

Closed brianwdavis closed 6 years ago

brianwdavis commented 6 years ago

https://github.com/baptiste/gridextra/blob/master/R/arrangeGrob.r#L153

These lines check if the top = argument is a character, and if so returns textGrob(top). If it's an expression, however, it doesn't do anything at all. The workaround for top = expression(xyz) is to call top = textGrob(expression(xyz)), but it feels to me like it should call it automatically.

p <- ggplot(mtcars, aes(disp, mpg)) + geom_point() + 
  stat_smooth(method = "lm", formula = y~poly(x, 2))

# works, but it's not parsed
grid.arrange(p, top = "y == b[0] + b[1]*x + b[2]*x^2")

# doesn't work, prints only p, no top grob
grid.arrange(p, top = expression(y == b[0] + b[1]*x + b[2]*x^2))

# works, but you have to set heights manually
grid.arrange(textGrob(expression(y == b[0] + b[1]*x + b[2]*x^2)), p)

# works, but it's more verbose
grid.arrange(p, top = textGrob(expression(y == b[0] + b[1]*x + b[2]*x^2)))
baptiste commented 6 years ago

i've implemented this with a more general test than just character; hopefully it won't break some corner case I haven't thought about.