R adding a datatable to a ggplot graph using viewPorts : Scaling the Grob

后端 未结 3 1897
情深已故
情深已故 2020-12-30 06:17

I\'m trying to add a data-table to a graph made in ggplot (similar to the excel functionality but with the flexibility to change the axis its on)

I\'ve had a few goe

3条回答
  •  爱一瞬间的悲伤
    2020-12-30 06:26

    You can try the new version of tableGrob; the resulting gtable width/height can be set to a specific size (here equi-distributed npc units)

    library(ggplot2)
    library(gridExtra)
    library(grid)
    tg <- tableGrob(head(iris), rows=NULL)
    tg$widths <- unit(rep(1/ncol(tg),ncol(tg)),"npc")
    tg$heights <- unit(rep(1/nrow(tg),nrow(tg)),"npc")
    
    qplot(colnames(iris), geom="bar")+ theme_bw() +
      scale_x_discrete(expand=c(0,0)) +
      scale_y_continuous(lim=c(0,2), expand=c(0,0)) +
      annotation_custom(ymin=1, ymax=2, xmin=-Inf, xmax=Inf, tg)
    

提交回复
热议问题