gpt4 book ai didi

r - 将矩阵添加到绘图中?

转载 作者:行者123 更新时间:2023-12-04 10:09:19 25 4
gpt4 key购买 nike

无论如何要在情节中添加表格。假设我有下面的情节:

curve(dnorm, -3, +4)

现在我想在图下方添加一个矩阵:
testMat <- matrix(1:20, ncol = 5)

我的目标?我正在编写一个绘图函数,它不仅可以创建绘图,而且还显示了一个矩阵,其中包含我感兴趣的绘图下方的信息。

请参阅附图以了解我的意思。
我真的很感谢你的帮助。

enter image description here

最佳答案

可能有更好的方法来做到这一点,但一种选择可能是使用“绘制”矩阵和数据框的软件包之一,例如 "gplots" package .

这是一个非常简单的示例(您可以自定义它以更好地控制最终布局)。

# Some sample data
testMat <- matrix(1:20, ncol = 5)
testMatDF <- as.data.frame(testMat)
names(testMatDF) <- c("Hey there", "Column 2",
"Some * Symbols", "And ^ More",
"Final Column")
rownames(testMatDF) <- paste("Group", 1:4)

# Load the package
library(gplots)
# Set par for plotting a three-row plot
par(mfrow = c(3, 1))
curve(dnorm, -3, +4)
textplot(testMat)
textplot(testMatDF)

结果:

enter image description here

您也可以使用 layout()而不是 par(mfrow...)如果您想在情节的放置方面更具创意。例如:
layout(matrix(c(1, 1, 2, 3, 3, 3), 
2, 3, byrow = TRUE))
curve(dnorm, -3, +4)
textplot(testMat)
textplot(testMatDF)

enter image description here

关于r - 将矩阵添加到绘图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12795828/

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