使用该par(mfrow = c(m,n))命令,我可以轻松制作带有m行和n列的图矩阵。
在特殊情况下,地块中存在一种模式,即每列中的所有地块共享一个重要属性,而每行中的所有地块共享一个不同的重要属性。所有这些信息都可以单独包含在每个m*n地块的标题中,但这显然是重复的。
是否有一种方便的方法可以在这样的网格中附加列名(仅在图的顶行上方)和行名(仅在图的左列的左侧)?
迄今为止的最佳解决方案:使用该text()命令将文本放置在左右图之外。但这并不令人满意,因为它需要许多单独的命令和调整参数,例如srt = 90使文本在左边距垂直,并xpd = NA在par().
lattice 和 ggplot2 包具有在网格中创建多个绘图的工具。如果它们适用于您想做的事情,它们可能会加快您的整个过程。
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)
Run Code Online (Sandbox Code Playgroud)
使用基本图形,您可以从设置外边距开始,查看命令的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))
Run Code Online (Sandbox Code Playgroud)