在 ggplot 中添加矩阵的数学符号

问题描述 投票:0回答:1

我想知道是否有一种方法可以编写矩阵的表达式,例如,一个 2 × 2 矩阵,并将其显示在图中,就像我在以下方程情况下可以简单地执行的方式一样:

$Y_{ij} = 3+2.5t_{ij} +\epsilon_{ij}$
R.

curve(dnorm, from = -3, to = 3, n = 1000, main = "Normal Probability Density Function")
text(-2, 0.3, expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                                        " ", e^{
                                            frac(-(x - mu)^2, 2 * sigma^2)
                                       })), cex = 1.2)
ggplot() +
 ggtitle(expression(paste(Y[ij] == beta[0]+beta[1]*t[ij] + b[0][i] + epsilon[ij])))
r ggplot2 latex
1个回答
0
投票

可以使用

gridExtra::tableGrob()
来绘制矩阵,即ggplot2 兼容(
grid
由ggplot2在后台使用)。 使用
patchwork
包,您可以将矩阵与 ggplot2 图结合起来。

library(patchwork)
library(ggplot2)
library(grid)
library(gridExtra)

matrix_grob <- tableGrob(matrix(1:4, 2, 2))

grid.newpage()
grid.draw(matrix_grob)


plot1 <- ggplot(mtcars, aes(cyl)) + geom_bar()
plot1 + matrix_grob

使用

grid::grid.arrange()
,你可以实现多种布局:

grid.arrange(grid.arrange(textGrob("This is a plot title and to the right is a matrix"),
                          matrix_grob,
                          ncol = 2,
                          widths = c(3, 1),
                          heights = 1),
             plot1,
             heights = c(1, 3),
             widths = 1)

© www.soinside.com 2019 - 2024. All rights reserved.