如何在 tableGrob 的底部添加一行?
How do i add a line to the bottom of a tableGrob?
假设我有一个 table 这样的:
df1 <- data.frame(x=1,y=1,z=1)
我把它变成了 table像这样的 Grob:
tableGrob(df1,rows = NULL) %>% 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 = 1, b = 1, l = 1, r = 3) %>% grid.draw()
或者像这样的第二行(将 t=1 更改为 t=2 并将 b=1 更改为 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()
但是当我尝试这个时(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 = 3, b = 3, l = 1, r = 3) %>% grid.draw()
我收到以下错误:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) : invalid 'layout.pos.row'
此外,这不起作用(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(2,"npc"),
x1 = unit(2,"npc"),
y1 = unit(2,"npc"),
gp = gpar(lwd = 2)),
t = 2, b = 2, l = 1, r = 3) %>% grid.draw()
有什么想法吗?为什么在底部添加一行的尝试特别失败?
Table
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)
假设我有一个 table 这样的:
df1 <- data.frame(x=1,y=1,z=1)
我把它变成了 table像这样的 Grob:
tableGrob(df1,rows = NULL) %>% 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 = 1, b = 1, l = 1, r = 3) %>% grid.draw()
或者像这样的第二行(将 t=1 更改为 t=2 并将 b=1 更改为 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()
但是当我尝试这个时(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 = 3, b = 3, l = 1, r = 3) %>% grid.draw()
我收到以下错误:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) : invalid 'layout.pos.row'
此外,这不起作用(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(2,"npc"),
x1 = unit(2,"npc"),
y1 = unit(2,"npc"),
gp = gpar(lwd = 2)),
t = 2, b = 2, l = 1, r = 3) %>% grid.draw()
有什么想法吗?为什么在底部添加一行的尝试特别失败?
Table
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)