- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难找到对齐 ggplot grob 和 table grob 的解决方案。我试图按照说明here但仍然没有给出我想要的结果。
library(grid)
library(gridExtra)
library(ggplot2)
library(tibble)
library(gtable)
dat <- tibble::rownames_to_column(mtcars, "car") #convert rownames to first col
plot1 <- ggplot(dat, aes(car, mpg)) +
geom_bar(stat = "identity") +
coord_flip()
g1 <- ggplotGrob(plot1)
tb1 <- tableGrob(dat$cyl)
g1 <- gtable_add_cols(g1, unit(0.2, "npc"))
g1 <- gtable_add_grob(g1, grobs = tb1, t=3, l=ncol(g1), b=6, r=ncol(g1))
grid.newpage()
grid.draw(g1)
我希望表格中的每个单元格都与直方图中的相关条对齐,但仍然无法理解如何从布局中实现 t,l,b,r。
This is the output I got
最佳答案
当我尝试使用 ggplot2 在 R 中制作类似森林图的东西时,我遇到了与上述类似的问题,但没有找到任何其他解决方案适合我的需求。上面的答案对我不起作用 - table 没有出现。所以我在代码上编写了一个不太漂亮的解决方案,但我实际上有点喜欢干净的视觉输出。
我喜欢这个解决方案的地方是:
library(gridExtra)
library(ggplot2)
dat <- data.frame(
label = c("A", "B", "C"),
point_est = c(1,2,3),
lb_ci = c(.5, 1.5, 2.5),
ub_ci = c(1.5, 2.5, 3.5),
n = c(50, 100, 150),
total = c(75, 150, 200)
)
plot1 <- ggplot(dat, aes(x=point_est, y=label)) +
geom_point() +
geom_errorbarh(aes(xmin=lb_ci, xmax=ub_ci), height=.5) +
ggtitle("Some measure") +
ylab(NULL) + xlab("some effect estimate")
tab_base <- ggplot(dat, aes(y=label)) +
ylab(NULL) + xlab(" ") +
theme(plot.title = element_text(hjust = 0.5, size=12), ## centering title on text
axis.text.x=element_text(color="white"), ## need text to be printed so it stays aligned with figure but white so it's invisible
axis.line=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())
tab1 <- tab_base +
geom_text(aes(x=1, label=n)) +
ggtitle("n")
tab2 <- tab_base +
geom_text(aes(x=1, label=total)) +
ggtitle("total")
lay <- matrix(c(1,1,1,1,1,1,2,3), nrow=1)
grid.arrange(plot1, tab1, tab2, layout_matrix = lay)
关于ggplot2 - ggplot grobs 与 tableGrob 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40265494/
我很惊讶以下从颜色向量创建的简单 grob 几乎按要求工作。 但是,我想让渐变从左到右,而不是从上到下。 library(ggplot2) library(grid) grad = colorRamp
我需要将 R 基础图转换为 grob,以便它可以叠加在一些 ggplot 上s。 我发现有几个函数可以做到这一点,ggplotify::as.grob和 cowplot::plot_to_gtable
我试图在 ggplot2 中生成一个轴线中断(轴线上有一个白色段),但遇到了一些问题。 使用信息性帖子 annotate-ggplot-with-an-extra-tick-and-label我能够在
这是 this question 的后续问题. OP 要求一种方法以特定距离安排地 block 的一部分。我认为 teunbrand 给出了一个很好的答案。 我自己的建议(用cowplot提取图例,并
披露:我不确定如何为这个问题制作一个可重现的示例。 我正在尝试使用gridExtra包绘制一个GROB列表。 我有一些代码,如下所示: ## Make Graphic Objects for Spec
我正在尝试绘制一个热图以及一行树状图,我对其进行了操作(修剪了分支数量),并使用grid.draw进行了对齐. 这是我的数据: set.seed(10) mat <- matrix(rnorm
我有一个 ggplot 对象: ggplot(plot1,aes(x = c, y = value, colour = variable, linetype = variable,size = var
所以我试图制作一个 grob 列表,然后将它们传递给 grobTree() ,但我的列表项不会被 do.call() 读入. 这是我的代码: library(purrr) library(grid)
我在画几个ggplot2对象并将它们放在 grid.arrange 上在对“pdf”设备的调用中。我发现如果我先对绘图进行光栅化,PDF 的性能会提高大约 10 亿倍(生成速度更快,渲染速度更快)。所
我有一个非常大的两个类别的散点图,其中一个点是“命中”。我想在图的顶部和侧面绘制直方图来表示在以下网站上看到的点击:http://blog.mckuhn.de/2009/09/learning-ggp
我正在尝试使用 ggplot2 重现以下 [base R] 图 我已经解决了大部分问题,但目前让我感到困惑的是将连接位于图右侧的边缘地毯图的线段放置在各自的标签上。标签是通过 anotation_cu
我正在尝试使用 grid.arrange 组合多种类型的图形/表格,其中之一是使用 corrplot 的相关矩阵。有没有办法将 corrplot 转换为 grob 或导出/导入为与 grid.arra
给定一个 ggplot,例如点,您如何找出给定点对应的数据行? 示例图: library(ggplot2) (p 为了回答 kohske 关于我为什么要这样做的问题,我使用 gridSVG 创建交互
我正在尝试使用 grid.arrange 组合多种类型的图形/表格,其中之一是使用 corrplot 的相关矩阵。有没有办法将 corrplot 转换为 grob 或导出/导入为与 grid.arra
如何在不一直使用 annotation_custom 的情况下在 ggplot 图表中包含多个 grobs 对象? 这是我的代码: df <- data.frame(x = 1:10, y = 1:1
如果对象包含我希望在分配时解析的其他变量,那么将 ggplot2 grob 分配给变量的正确方法是什么。 例如: xpos和 ypos是我想要解决的值。 我将 geom_text 分配给 ptext,
更新原来是无法通过远程桌面连接生成光栅图形的问题。 我想将文件中的图像插入到使用 ggplot2 绘制的绘图中。这个问题已经在这里问过(Inserting an image to ggplot2),但
我很难找到对齐 ggplot grob 和 table grob 的解决方案。我试图按照说明here但仍然没有给出我想要的结果。 library(grid) library(gridExtra) li
R 图形具有 grid 包,理论上允许使用 grid.curve 在形状之间创建弯曲箭头(请参阅此处的 grid.curve 描述 https://www.stat.auckland.ac.nz/~p
在对齐网格图形对象时遇到了一些麻烦——我已经阅读了我能找到的所有文档,包括 Murrell 的书,但没有成功。我认为我正在尝试做的事情非常简单,所以希望我缺少简单的东西。 这是一个可重现的示例,它将按
我是一名优秀的程序员,十分优秀!