Adding a matrix to the plot?

Is it possible to add a table to the plot. Suppose I have a chart below:

curve(dnorm, -3, +4) 

Now I like to add a matrix to the plot:

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

My goal? I am writing a plot function that not only creates a plot, but also displays a matrix that includes information that interests me in the plot.

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

enter image description here

+7
source share
2 answers

There are probably more efficient ways to do this, but one option would be to use one of the packages that โ€œdisplaysโ€ matrices and data frames, for example, the โ€œgplotsโ€ package .

Here is a very simple example (perhaps you can tweak it for 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) 

Result:

enter image description here

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

 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

+7
source

The plotrix package provides the addtable2plot function.

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: Place the table in a new chart to place it under 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") 
+7
source

All Articles