Make a graph matrix with row and column names

Using the command par(mfrow = c(m,n)), I can easily create a graph matrix with mrows and columns n.

In special cases, there is a template on the graphs, so that all graphs in each column have an important attribute, and all graphs in each row have a different important attribute. All this information can be included in the title of each of the m*ngraphs individually, but this, obviously, is repeated.

Is there a convenient way to add column names (only above the top row of graphs) and row names (only to the left of the left column of graphs) in such a grid?

The best solution so far . Use the command text()to place text outside the left and top charts. But this is rather unsatisfactory, since this requires many separate commands and settings such as the srt = 90text to be vertical in the left margin, and using xpd = NAinside par().

+4
source share
1 answer

Lattice and ggplot2 packages have tools for creating multiple graphs in a grid. They can speed up the whole process if they apply to what you want to do.

library(lattice)

splom( ~ iris[,1:4], data=iris, groups=Species )
xyplot( mpg ~ wt | factor(cyl)*factor(am), data=mtcars )

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(am ~ cyl)

, , oma par, mtext, .

par( oma=c(0,6,6,0), mfrow=c(2,2), mar=c(2,2,1,1)+0.1 )
with(iris, plot(Sepal.Width, Petal.Width, ann=FALSE))
mtext( 'Width', side=3, line=2, at=grconvertX(0.5,'npc','nic'), outer=TRUE )
mtext( 'Width', side=2, line=2, at=grconvertY(0.5,'npc','nic'), outer=TRUE )
mtext( 'Sepal', side=3, line=4, outer=TRUE, cex=2 )
mtext( 'Petal', side=2, line=4, outer=TRUE, cex=2 )
with(iris, plot(Sepal.Length, Petal.Width, ann=FALSE))
mtext( 'Length', side=3, line=2, at=grconvertX(0.5,'npc','nic'), outer=TRUE )
with(iris, plot(Sepal.Width, Petal.Length, ann=FALSE))
mtext( 'Length', side=2, line=2, at=grconvertY(0.5, 'npc','nic'), outer=TRUE )
with(iris, plot(Sepal.Length, Petal.Length, ann=FALSE))
+4

Source: https://habr.com/ru/post/1546180/


All Articles