wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Rename and enchance ggplot_to_gtable → plot_to_gtable #68

Closed flying-sheep closed 7 years ago

flying-sheep commented 7 years ago

plot_grid and friends now also accept default R plots as recordedplot object or function that plots when called.

This PR also contains a few R CMD check fixes in other commits.

Fixes #52

clauswilke commented 7 years ago

Thanks for your effort on this!

Two requests:

flying-sheep commented 7 years ago

OK, done

clauswilke commented 7 years ago

Great, thanks!

clauswilke commented 7 years ago

Hey,

I've played around a lot with the recording of base plots, and I've encountered some problems. For example, for plot(sqrt), the axis labels tend to be partially or completely cut off:

p <- function() plot(sqrt)
ggdraw(p)

rplot

Also, the plot can affect neighboring plots when used in plot_grid(), even though it shouldn't extend into the neighboring space. See e.g. this example taken from the vignette here:

screen shot 2017-07-28 at 4 53 34 pm

The legend title is partially cut off. This problem goes away if I swap the two plots, demonstrating that the recorded plot somehow overlaps with the ggplot2 plot.

Not sure where the problem resides, because when I call gridEcho(p) for the above code example the plot looks correct. So the problem has to be somehow in the conversion of the gridEcho() output to a grobTree. Any insight you can provide would be appreciated.

flying-sheep commented 7 years ago

hi, i’m happy to help, but i fear there are too many things i don’t understand.

For example, for plot(sqrt), the axis labels tend to be partially or completely cut off:

p <- function() plot(sqrt)
ggdraw(p)

hmm, doesn’t happen for me. is this happening in RStudio, plain R, or where?

image

Also, the plot can affect neighboring plots when used in plot_grid(), even though it shouldn't extend into the neighboring space.

why can this happen? i thought plot_grid takes a bunch of gtables or other grid stuff and puts them into a grid. shouldn’t the drawing mechanism prevent that?

clauswilke commented 7 years ago

It seems the problems are slightly different depending on whether I run the code in RStudio or plain R. But there are problems nonetheless. In plain R, this code works as expected:

p <- function() plot(sqrt)
ggdraw(p)

However, when I put the same plot into the plot grid, it doesn't work properly. The axis labels are now missing.

plot_grid(p, p, p, p, scale = .8)
screen shot 2017-07-29 at 11 23 20 am

The same happens when I use the draw_plot() function:

ggdraw() + draw_plot(p)
screen shot 2017-07-29 at 11 27 52 am

All these functions internally call annotation_custom(). The difference is that the version in ggdraw() leaves the xmin, xmax, ymin, and ymax coordinates at their defaults while draw_plot() sets them to the limits of the coordinate system set up by ggdraw(). So it's some problem with viewports and coordinate system, somewhere in annotation_custom() or deeper in grid.

Btw., grobs can in principle draw outside their assigned area when placed with draw_plot() / annotation_custom(), because ggplot2 doesn't enforce clipping inside the plot area. This could be changed in cowplot, but I don't think clipping is the problem. The problem is that somehow things live at strange coordinates in the grobs created by grid.grabExpr(), possibly at coordinates -Inf or Inf.

clauswilke commented 7 years ago

Indeed, for example the y axis label lives at a negative x coordinate:

screen shot 2017-07-29 at 11 39 03 am

So some additional code will be necessary to make sure we capture the complete grob and properly move it into a box that completely encloses it. I'll think about it some more, but would appreciate any additional help you can provide.

clauswilke commented 7 years ago

Ah, one more thing: which parts are cut off seems to depend on the exact size and aspect ratio of the device into which the captured plot is drawn. Do the following experiment. Rerun this code in plain R:

p <- function() plot(sqrt)
ggdraw(p)

and then resize the plot window that pops up. You can see how different parts of the plot are cut off depending on how exactly the window is resized.

clauswilke commented 7 years ago

I'm moving this discussion to issue #69.