Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在表格GROB的顶部添加一条线,并将表格插入另一个绘图_R_Ggplot2_Grob - Fatal编程技术网

在表格GROB的顶部添加一条线,并将表格插入另一个绘图

在表格GROB的顶部添加一条线,并将表格插入另一个绘图,r,ggplot2,grob,R,Ggplot2,Grob,两个问题: 如何在使用tableGrob制作的表格顶部添加一行 这是我的代码和数据: library(gridExtra) library(grid) library(gtable) find_cell <- function(table, row, col, name="core-bg"){ l <- table$layout which(l$t==row & l$l==col & l$name==name) } tt3 <-

两个问题:

  • 如何在使用
    tableGrob
    制作的表格顶部添加一行
  • 这是我的代码和数据:

    library(gridExtra)
    library(grid)
    library(gtable)
    
    find_cell <- function(table, row, col, name="core-bg"){
      l <- table$layout
      which(l$t==row & l$l==col & l$name==name)
    }
    
    tt3 <- ttheme_minimal(base_size = 8,
                          colhead = list(fg_params = list(fontface=c(1,3,3))))
    
    lintable <- data.frame("Comparison" =   c("Early/Late", "Early/Tips",   "Late/Tips"),
                           "Z" =    c(-2.098,   -6.077, -3.57),
                           "p" =    c(0.036,    "< 0.001",  "< 0.001"))
    
    lint <- tableGrob(lintable, rows = NULL, theme = tt3)
    
    ind <- find_cell(lint, 2, 3, "core-bg")
    ind1 <- find_cell(lint, 3, 3, "core-bg")
    ind2 <- find_cell(lint, 4, 3, "core-bg")
    lint$grobs[ind][[1]][["gp"]] <- gpar(fill="gray83", col = NA)
    lint$grobs[ind1][[1]][["gp"]] <- gpar(fill="gray83", col = NA)
    lint$grobs[ind2][[1]][["gp"]] <- gpar(fill="gray83", col = NA)
    lint1 <- gtable_add_grob(lint,
                             grobs = segmentsGrob(
                               x0 = unit(0,"npc"),
                               y0 = unit(0,"npc"),
                               x1 = unit(1,"npc"),
                               y1 = unit(0,"npc"),
                               gp = gpar(lwd = 1)),
                             t = 1, l = 1, r = ncol(lint))
    lint2 <- gtable_add_grob(lint1,
                             grobs = segmentsGrob(
                               x0 = unit(0,"npc"),
                               y0 = unit(0,"npc"),
                               x1 = unit(1,"npc"),
                               y1 = unit(0,"npc"),
                               gp = gpar(lwd = 1)),
                             t = 4, l = 1, r = ncol(lint1))
    lint3 <- gtable_add_grob(lint2,
                             grobs = segmentsGrob(
                               x0 = unit(0,"npc"),
                               y0 = unit(0,"npc"),
                               x1 = unit(1,"npc"),
                               y1 = unit(0,"npc"),
                               gp = gpar(lwd = 1)),
                             t = 0, l = 1, r = ncol(lint2))
    
    grid.draw(lint3)
    
    R似乎不认为该表是grob(但
    is.grob
    返回为
    TRUE
    ),这似乎导致了问题。不幸的是,我不知道从哪里开始解决这个问题

    任何问题的帮助都将不胜感激

    非常感谢,

    卡罗莱纳州

    编辑:我用以下代码算出了第二部分:

    pushViewport(viewport(x=0.1, y=0.77, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint2)
    pushViewport(viewport(x=0.37, y=-0.97, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint1)
    pushViewport(viewport(x=0.37, y=-4.9, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint)
    

    问题是,如果我以任何方式调整视口,则插图不会停留在我放置它们的相对位置。是否有一种方法可以永久固定插图,以便在移动视口时,它们相对于绘图的其余部分保持在同一位置?

    您可以在表格上绘制如下线条:


    lint1您可以这样在表上画线:


    lint1太好了,谢谢!我试着用你的建议,但也没能让它们发挥作用。。。至少现在我有了完成工作的工作代码!:)太好了,谢谢!我试着用你的建议,但也没能让它们发挥作用。。。至少现在我有了完成工作的工作代码!:)
    
    pushViewport(viewport(x=0.1, y=0.77, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint2)
    pushViewport(viewport(x=0.37, y=-0.97, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint1)
    pushViewport(viewport(x=0.37, y=-4.9, w=0.25, h=0.25, just=c("left", "bottom")))
    grid.draw(lint)