gpt4 book ai didi

R ggplot 和 gt 输出 - 如何将它们组合到输出图像上

转载 作者:行者123 更新时间:2023-12-04 16:37:08 26 4
gpt4 key购买 nike

在基础 R 中,我有一些代码可以在图表下方写入统计数据表。我想用“ggplot”图表和“gt”表做一个类似的过程。解决这个问题的最佳方法是什么?我在下面使用 gridExtra 的尝试失败了。

# load requried packages
require(tidyverse)
require(gt)
require(gridExtra)

# make a ggplot chart
GGP <- ggplot(dat = iris, aes( x= Sepal.Width, y = Sepal.Length, colour = Species)) + geom_point()

# make a dt statistics table
GT <- gt(iris %>% group_by(Species) %>% summarise(n = n(), Mean = mean(Sepal.Width), SD = sd(Sepal.Width))

# Plot both on one page?
grid.arrange(GGP, GT, nrow = 2)

最佳答案

使用 grid.arrange 组合绘图, 两个对象都需要是图形对象或 grobs ,以及来自 gt 的输出不是。一种方法是使用 tableGrob()来自 gridExtra创建grob:

tab = iris %>% 
group_by(Species) %>%
summarise(n = n(), Mean = mean(Sepal.Width), SD = sd(Sepal.Width))

grid.arrange(GGP,tableGrob(tab))
enter image description here

关于R ggplot 和 gt 输出 - 如何将它们组合到输出图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68517917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com