在 tableGrob 的顶部添加一行并将 table 插入另一个绘图
Adding a line to the top of a tableGrob and insetting the table into another plot
两个问题:
- 如何在使用
tableGrob
制作的 table 顶部添加一行?
这是我的代码和我的数据:
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)
但是我得到这个错误:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) :
invalid 'layout.pos.row'
我已经检查过,代码可以正常工作,包括制作 lint2
。该错误与 lint3
有关。我知道这是因为我指定了 t = 0
但我不知道如何指定第一行的上边框。
- 如何将上面的 table 插入到由多个其他地块组成的另一个地块中?
将 table 插入的绘图代码如下:
library(ggpubr)
a <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
ap <- ggplot(a, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
b <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
bp <- ggplot(b, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
c <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
cp <- ggplot(c, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
gt <- arrangeGrob(ap,bp,cp,
layout_matrix = rbind(c(1),c(2),c(3)))
p <- as_ggplot(gt) +
draw_plot_label(label = c("A", "B", "C"), size = 12,
x = c(0, 0, 0), y = c(1, 0.66, 0.33))
p
R 似乎并不认为 table 是一个 grob(但 is.grob
returns 是 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)
问题是如果我以任何方式调整视口,插图不会停留在我放置它们的相对位置。有没有办法将插图永久固定到位,这样如果我移动视口,它们相对于绘图的其余部分保持在同一个位置?
您可以像这样在 table 上画线:
lint1 <- gtable_add_grob(lint,
grobs = segmentsGrob(
x0 = unit(0,"npc"),
y0 = unit(1,"npc"),
x1 = unit(1,"npc"),
y1 = unit(1,"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(1,"npc"),
x1 = unit(1,"npc"),
y1 = unit(1,"npc"),
gp = gpar(lwd = 1)),
t = 2, 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 = 4, l = 1, r = ncol(lint2))
grid.newpage()
grid.draw(lint3)
似乎你通过直接使用 grid
而不是更高级别的接口(例如来自 ggpmisc
的 geom_table
和使用 [=14= 的情节安排让事情变得更难了]
两个问题:
- 如何在使用
tableGrob
制作的 table 顶部添加一行?
这是我的代码和我的数据:
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)
但是我得到这个错误:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) : invalid 'layout.pos.row'
我已经检查过,代码可以正常工作,包括制作 lint2
。该错误与 lint3
有关。我知道这是因为我指定了 t = 0
但我不知道如何指定第一行的上边框。
- 如何将上面的 table 插入到由多个其他地块组成的另一个地块中?
将 table 插入的绘图代码如下:
library(ggpubr)
a <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
ap <- ggplot(a, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
b <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
bp <- ggplot(b, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
c <- data.frame("group2" = letters[1:2],
"Rate" = sample(0:100, 20, replace = TRUE))
cp <- ggplot(c, aes(x=group2, y=Rate))+
geom_boxplot(show.legend = F, fill = "gray83", lwd=0.2, fatten = 1)+
ylab("Rate")+
theme_classic()+
theme(axis.title.x=element_blank())+
theme(text = element_text(size=12))+
theme(axis.title=element_text(size=11))
gt <- arrangeGrob(ap,bp,cp,
layout_matrix = rbind(c(1),c(2),c(3)))
p <- as_ggplot(gt) +
draw_plot_label(label = c("A", "B", "C"), size = 12,
x = c(0, 0, 0), y = c(1, 0.66, 0.33))
p
R 似乎并不认为 table 是一个 grob(但 is.grob
returns 是 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)
问题是如果我以任何方式调整视口,插图不会停留在我放置它们的相对位置。有没有办法将插图永久固定到位,这样如果我移动视口,它们相对于绘图的其余部分保持在同一个位置?
您可以像这样在 table 上画线:
lint1 <- gtable_add_grob(lint,
grobs = segmentsGrob(
x0 = unit(0,"npc"),
y0 = unit(1,"npc"),
x1 = unit(1,"npc"),
y1 = unit(1,"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(1,"npc"),
x1 = unit(1,"npc"),
y1 = unit(1,"npc"),
gp = gpar(lwd = 1)),
t = 2, 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 = 4, l = 1, r = ncol(lint2))
grid.newpage()
grid.draw(lint3)
似乎你通过直接使用 grid
而不是更高级别的接口(例如来自 ggpmisc
的 geom_table
和使用 [=14= 的情节安排让事情变得更难了]