Removing one tableGrob when applied to a box plot with a facet_wrap

后端 未结 2 1089
梦谈多话
梦谈多话 2020-12-01 22:37

I\'m using the code below to enrich a box plot with a summary table for categorical variable created on the x-axis.

# Libs
require(ggplot2); require(gridExtr         


        
2条回答
  •  無奈伤痛
    2020-12-01 23:01

    This is just an illustration of the comment.

    ggp <- ggplot(data = mtcars, aes(x = factor(cyl), y = qsec, fill = as.factor(gear))) +
      geom_boxplot() +
      scale_y_continuous(limits = quantile(mtcars$qsec, c(0.1, 0.9))) +
      scale_fill_tableau("gear",palette = "tableau10") +
      xlab("cyl") + ylab("qsec") +
      facet_wrap(~am) 
    
    # this requires gridExtra 2.0.0
    tt <- ttheme_default(core    = list(fg_params=list(cex = 0.7)),
                         colhead = list(fg_params=list(cex = 0.7)))
    grid.newpage()
    grid.draw(arrangeGrob(ggp))
    grid.draw(grobTree(tableGrob(fun_dta_sum(var_sum = mtcars$qsec, group = mtcars$cyl, data = mtcars),
                                 rows=NULL, theme=tt), 
                       vp=viewport(x=unit(0.20,"npc"),y=unit(0.20,"npc"))))
    

    The point is that you really just need to tweak the x=... and y=... arguments to viewport(...). Using annotation_custom(...), even if you could hack the gTable to get rid of one of the grobs, you would still need to tweak the position (using xmin=... and ymin=...). This approach does not maintain the relative position when you shrink or enlarge the image, but neither does annotation_custom(...), so overall I don't really see this as any more difficult.

提交回复
热议问题