如何在tableGrob的底部添加一行?

时间:2017-12-20 13:24:14

标签: r gridextra r-grid

说我有这样一张桌子:

targetSdkVersion

然后我把它变成了一个像这样的tableGrob:

Activity

如何在底部添加一行?

我可以像这样在顶部添加一行:

df1 <- data.frame(x=1,y=1,z=1)

或者像这样的第二行(改变t = 1到t = 2,b = 1到b = 2):

tableGrob(df1,rows = NULL) %>% grid.draw()

但是当我尝试这个时(t = 3和b = 3):

tableGrob(df1,rows = NULL) %>%   gtable_add_grob(
grobs = segmentsGrob( # line across the bottom
  x0 = unit(0,"npc"),
  y0 = unit(1,"npc"),
  x1 = unit(2,"npc"),
  y1 = unit(1,"npc"),
  gp = gpar(lwd = 2)),
t = 1, b = 1, l = 1, r = 3) %>% grid.draw()

我收到以下错误:

tableGrob(df1,rows = NULL) %>%   gtable_add_grob(
grobs = segmentsGrob( # line across the bottom
  x0 = unit(0,"npc"),
  y0 = unit(1,"npc"),
  x1 = unit(2,"npc"),
  y1 = unit(1,"npc"),
  gp = gpar(lwd = 2)),
t = 2, b = 2, l = 1, r = 3) %>% grid.draw()

此外,这不起作用(t = 2且b = 2,但y0 = 2且y1 = 2):

tableGrob(df1,rows = NULL) %>%   gtable_add_grob(
grobs = segmentsGrob( # line across the bottom
  x0 = unit(0,"npc"),
  y0 = unit(1,"npc"),
  x1 = unit(2,"npc"),
  y1 = unit(1,"npc"),
  gp = gpar(lwd = 2)),
t = 3, b = 3, l = 1, r = 3) %>% grid.draw()

有什么想法吗?为什么在底部添加一行的尝试特别失败?

1 个答案:

答案 0 :(得分:1)

df1 <- data.frame(x=1,y=1,z=1)

分配给变量g1

g1 <- tableGrob(df1,rows = NULL)


g1 <- gtable_add_grob(g1,
                     grobs = segmentsGrob( # line across the bottom
                       x0 = unit(0,"npc"),
                       y0 = unit(0,"npc"),
                       x1 = unit(1,"npc"),
                       y1 = unit(0,"npc"),
                       gp = gpar(lwd = 2.0)),
                     t = 2, b = 2, l = 1, r = ncol(g1))
grid.draw(g1)

enter image description here