wilkelab / cowplot

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

Incorporation of base-R plots into `plot_grid()` can lead to cut-off labels or other drawing artifacts. #69

Closed clauswilke closed 6 years ago

clauswilke commented 7 years ago

See discussion here.

Tagging @flying-sheep who is part of this discussion.

clauswilke commented 7 years ago

I've made a branch draw_grob to investigate what's going on. Fixing this will likely require re-implementation of the draw_grob function, if it is possible to fix at all.

One thing I've learned already is that clipping doesn't work very well in grid, because nested clip areas aren't possible. And, gtable_col() by default switches off any clipping that may have been set. See this issue I've filed for gtable.

clauswilke commented 7 years ago

Merged the branch back into master. draw_grob is now reimplemented, which was useful for several reasons. However, the issue of drawing artifacts may not be solvable.

clauswilke commented 7 years ago

I have solved the problem. The issue can be avoided through appropriate settings in par() before plotting.

p1 <- ggplot(mpg, aes(displ, cty, color=factor(cyl))) + geom_point()
dev.new()
par(xpd = NA, # switch off clipping, necessary to always see axis labels
    bg = "transparent", # switch off background to avoid obscuring adjacent plots
    oma = c(2, 2, 0, 0)) # move plot to the right and up
plot(sqrt)
p6 <- recordPlot()
dev.off()
p7 <- function() image(volcano)
p8 <- gtable::gtable_col("circle", list(grid::circleGrob()))

plot_grid(p1, p6, p7, p8, scale = c(1, .9, .9, .9))
screen shot 2017-07-29 at 6 27 46 pm
flying-sheep commented 7 years ago

this is amazing, thank you! looks so good.

is it possible to do the clipping and background automatically? i assume they’ll be always useful in the context of cowplot.

the problem seems to always appears with recordedplot instances. so maybe we could set the pars when replaying those?

GuangchuangYu commented 6 years ago

I did something similar a years ago, when I try to let base plot users can also generate a hexsticker in R using my hexSticker package.

I just got an idea of making it to be supported by cowplot and created the base2grob without aware of this (thanks @clauswilke for the link).

clauswilke commented 6 years ago

Closing this issue. There have been huge improvements on this front, and this now generally works without problems.

p1 <- ggplot(mpg, aes(displ, cty, color=factor(cyl))) + geom_point()
p2 <- ~plot(sqrt)
p3 <- function() image(volcano)
p4 <- gtable::gtable_col("circle", list(grid::circleGrob()))

plot_grid(p1, p2, p3, p4)
screen shot 2018-05-05 at 5 35 49 pm

Also, if the drawing area becomes too small, cowplot notices and displays an error.

screen shot 2018-05-05 at 5 37 26 pm
flying-sheep commented 6 years ago

awesome!