Figure caption scientific names + symbols in textGrob gtable

后端 未结 2 1270
一生所求
一生所求 2021-01-14 17:25

First of all I would like to thank Sir Baptiste for helping me improve my R script by adding a caption at the bottom left the of the combined plots using gtable/textGrob as

2条回答
  •  孤独总比滥情好
    2021-01-14 17:52

    Based on the comments, I suggest the following strategy: create a dummy plot with your figure caption (text) as legend title, extract its legend, and place it at the bottom of your gtable.

    library(grid)
    library(gridExtra)
    library(ggplot2)
    library(gtable)
    
    p1 <- ggplot()
    p2 <- ggplot(ToothGrowth, aes(len, dose, shape=supp)) + geom_point() +
      theme(legend.position="bottom", 
            legend.background=element_rect(colour="black")) 
    
    title <- expression("Figure 1. This "*italic(is)*" now a legendary caption")
    dummy <- ggplotGrob(p2 + guides(shape = guide_legend(title = title)))
    
    g1 <- ggplotGrob(p1)
    g2 <- ggplotGrob(p2)
    caption <- gtable_filter(dummy,"guide")[["grobs"]][[1]]
    caption$widths <- grid:::unit.list(caption$widths)
    caption$widths <- unit.c(unit(0,"mm"), caption$widths[2], unit(1,"null"))
    
    g <- rbind(g1, g2)
    g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)
    g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))
    grid.newpage()
    grid.draw(legend)
    grid.draw(g)
    

提交回复
热议问题