在绘图中添加矩阵?

时间:2022-10-04 14:56:41

Is there anyway to add a table to a plot. Suppose that I have the plot below:

无论如何都要在剧情中添加一张桌子。假设我有以下情节:

curve(dnorm, -3, +4)

Now I like to add a matrix beneath the plot:

现在我想在情节下面添加一个矩阵:

testMat <- matrix(1:20, ncol = 5)

My goal? I'm writing a plot function that not only does create a plot, but it also shows a matrix including the information I'm interested in underneath the plot.

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

Please see the attached picture to see what I mean. I really appreciate your help.

请参阅附图了解我的意思。我非常感谢你的帮助。

在绘图中添加矩阵?

2 个解决方案

#1


7  

There are probably better ways to do this, but one option might be to use one of the packages that "plots" matrices and data frames, like the "gplots" package.

可能有更好的方法来做到这一点,但一种选择可能是使用“绘制”矩阵和数据框的一个包,如“gplots”包。

Here is a very bare example (you can probably customize this for much finer control over the final layout).

这是一个非常简单的例子(你可以自定义它,以便对最终布局进行更精细的控制)。

# 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)

The result:

在绘图中添加矩阵?

You can also use layout() instead of par(mfrow...) if you want to get a little bit more creative with the placement of your plots. For example:

如果您希望通过放置图表来获得更多创意,也可以使用layout()而不是par(mfrow ...)。例如:

layout(matrix(c(1, 1, 2, 3, 3, 3), 
              2, 3, byrow = TRUE))
curve(dnorm, -3, +4)
textplot(testMat)
textplot(testMatDF)

在绘图中添加矩阵?

#2


7  

Package plotrix provides function addtable2plot.

Package plotrix提供addtable2plot功能。

Example from the help file:

帮助文件中的示例:

library(plotrix)
testdf<-data.frame(Before=c(10,7,5),During=c(8,6,2),After=c(5,3,4))
rownames(testdf)<-c("Red","Green","Blue")
barp(testdf,main="Test addtable2plot",ylab="Value",
     names.arg=colnames(testdf),col=2:4)
# show most of the options
addtable2plot(2,8,testdf,bty="o",display.rownames=TRUE,hlines=TRUE,
              title="The table")

Edit: Put the table in a new plot to place it underneath your plot.

编辑:将表格放入新图表中,将其放置在图表下方。

library(plotrix)

layout(matrix(c(1,2), 2, 1, byrow = TRUE),
       widths=c(1,1), heights=c(2,1))


testdf<-data.frame(Before=c(10,7,5),During=c(8,6,2),After=c(5,3,4))
rownames(testdf)<-c("Red","Green","Blue")
barp(testdf,main="Test addtable2plot",ylab="Value",
     names.arg=colnames(testdf),col=2:4)

plot.new()
addtable2plot(0,0,testdf,bty="o",display.rownames=TRUE,hlines=TRUE,
              title="The table")

#1


7  

There are probably better ways to do this, but one option might be to use one of the packages that "plots" matrices and data frames, like the "gplots" package.

可能有更好的方法来做到这一点,但一种选择可能是使用“绘制”矩阵和数据框的一个包,如“gplots”包。

Here is a very bare example (you can probably customize this for much finer control over the final layout).

这是一个非常简单的例子(你可以自定义它,以便对最终布局进行更精细的控制)。

# 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)

The result:

在绘图中添加矩阵?

You can also use layout() instead of par(mfrow...) if you want to get a little bit more creative with the placement of your plots. For example:

如果您希望通过放置图表来获得更多创意,也可以使用layout()而不是par(mfrow ...)。例如:

layout(matrix(c(1, 1, 2, 3, 3, 3), 
              2, 3, byrow = TRUE))
curve(dnorm, -3, +4)
textplot(testMat)
textplot(testMatDF)

在绘图中添加矩阵?

#2


7  

Package plotrix provides function addtable2plot.

Package plotrix提供addtable2plot功能。

Example from the help file:

帮助文件中的示例:

library(plotrix)
testdf<-data.frame(Before=c(10,7,5),During=c(8,6,2),After=c(5,3,4))
rownames(testdf)<-c("Red","Green","Blue")
barp(testdf,main="Test addtable2plot",ylab="Value",
     names.arg=colnames(testdf),col=2:4)
# show most of the options
addtable2plot(2,8,testdf,bty="o",display.rownames=TRUE,hlines=TRUE,
              title="The table")

Edit: Put the table in a new plot to place it underneath your plot.

编辑:将表格放入新图表中,将其放置在图表下方。

library(plotrix)

layout(matrix(c(1,2), 2, 1, byrow = TRUE),
       widths=c(1,1), heights=c(2,1))


testdf<-data.frame(Before=c(10,7,5),During=c(8,6,2),After=c(5,3,4))
rownames(testdf)<-c("Red","Green","Blue")
barp(testdf,main="Test addtable2plot",ylab="Value",
     names.arg=colnames(testdf),col=2:4)

plot.new()
addtable2plot(0,0,testdf,bty="o",display.rownames=TRUE,hlines=TRUE,
              title="The table")