user127126
user127126

Reputation: 43

How do i add a line to the bottom of a tableGrob?

Say I have a table like this:

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

And I turn it into a tableGrob like this:

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

How do I add a line to the bottom?

I can add a line to the top like this:

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()

Or to the second line like this (changed t=1 to t=2 and b=1 to b=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 = 2, b = 2, l = 1, r = 3) %>% grid.draw()

But when I try this (t=3 and 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 = 3, b = 3, l = 1, r = 3) %>% grid.draw()

I get the following error:

Error in grid.Call.graphics(C_setviewport, vp, TRUE) :   invalid 'layout.pos.row'

Also, this does not work (t=2 and b=2, but y0=2 and y1=2):

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

Any ideas? Why specifically did the attempts to add a line at the bottom fail?

Upvotes: 1

Views: 1761

Answers (1)

dondapati
dondapati

Reputation: 849

Table

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

Assign to the Variable 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

Upvotes: 1

Related Questions