使用 par(mfrow=c(x,y)) 生成的图的每个面板上的标题键 [英] Title key on each panel of a plot generated with par(mfrow=c(x,y))

查看:26
本文介绍了使用 par(mfrow=c(x,y)) 生成的图的每个面板上的标题键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在科学出版物中使用,我想在用 par(mfrow=c(3,1)) 生成的一个图中的三个图中添加一个键(A、B、C)作为标题".向单个图添加漂亮的标题"键的一个非常简单的解决方案是使用 title() 的outer"参数(例如 title(outer=T,adj=0,main="A",cex=1.1, col="black",font=2,line=-1)) 但是当这用于多个绘图时,所有标题都在同一个地方结束:

par(mfrow=c(3,1))情节(rnorm(100),col =红色")标题(外=T,adj=0,main="A",cex=1.1,col="black",font=2,line=-1)情节(rnorm(100),col =蓝色")标题(外=T,adj=0,main="B",cex=1.1,col="black",font=2,line=-1)情节(rnorm(100),col =绿色")标题(外=T,adj=0,main="C",cex=1.1,col="black",font=2,line=-1)

我知道我可以使用 layout 和/或 mtext (

adj 可以使用负值.

For use in a scientific publication I would like to add a key (A, B, C) as a 'title' to three plots in one graph generated with par(mfrow=c(3,1)). A very easy solution to add a nice 'title' key to a single plot is to use the 'outer' parameter of the title() (e.g. title(outer=T,adj=0,main="A",cex=1.1, col="black",font=2,line=-1)) however when this would be used for multiple plots, all titles end up in the same place:

par(mfrow=c(3,1))
plot(rnorm(100),col="red")
title(outer=T,adj=0,main="A",cex=1.1,col="black",font=2,line=-1)
plot(rnorm(100),col="blue")
title(outer=T,adj=0,main="B",cex=1.1,col="black",font=2,line=-1)
plot(rnorm(100),col="green")
title(outer=T,adj=0,main="C",cex=1.1,col="black",font=2,line=-1)

I am aware that I could use layout and/or mtext (Common main title of a figure panel compiled with par(mfrow)) however I feel there might be an easier solution that I am overlooking (I have no acces to Adobe Illustrator and would like to use R to generate the graph completely). The solution I can get with the use of layout is not good enough (key is too much separated from the plot and not enough to the left), also I find the playing around with the margins and coordinates a bit too tedious.

par(mar=c(1,2,2,1))
layout(matrix(seq(1,6),ncol=1),heights=rep(c(1,3),3))
plot.new()
text(0,0.5,"A",cex=1.1,font=2)
plot(rnorm(100),col="red")
plot.new()
text(0,0.5,"B",cex=1.1,font=2)
plot(rnorm(100),col="blue")
plot.new()
text(0,0.5,"C",cex=1.1,font=2)
plot(rnorm(100),col="blue")

Any help would be much appreciated. Even if the answer would be that this is simply not possible in an easy way, that would still be very helpful.

解决方案

When you use outer=TRUE, you are asking to write the title in the outer margin (common to all sub-plots). To do what you want, just set outer=FALSE:

outer = FALSE

line = -2
cex = 2
adj  = 0.025

par(mfrow=c(3,1))
plot(rnorm(100),col="red")
title(outer=outer,adj=adj,main="A",cex.main=cex,col="black",font=2,line=line)
plot(rnorm(100),col="blue")
title(outer=outer,adj=adj,main="B",cex.main=cex,col="black",font=2,line=line)
plot(rnorm(100),col="green")
title(outer=outer,adj=adj,main="C",cex.main=cex,col="black",font=2,line=line)

Also, if you want the labels to be in the side, you can use mtext instead title:

line = 6
cex = 2
las = 2

par(mfrow=c(3,1), oma=c(1,6,1,1))

plot(rnorm(100),col="red")
mtext("A", side=2, line=line, cex=cex, las=las)
plot(rnorm(100),col="blue")
mtext("B", side=2, line=line, cex=cex, las=las)
plot(rnorm(100),col="green")
mtext("C", side=2, line=line, cex=cex, las=las)

Another option, to have the labels in the corner, is:

line = 1
cex = 2
side = 3
adj=-0.05

par(mfrow=c(3,1), oma=c(1,6,1,1))

plot(rnorm(100),col="red")
mtext("A", side=side, line=line, cex=cex, adj=adj)
plot(rnorm(100),col="blue")
mtext("B", side=side, line=line, cex=cex, adj=adj)
plot(rnorm(100),col="green")
mtext("C", side=side, line=line, cex=cex, adj=adj)

It is possible to use negative values for adj.

这篇关于使用 par(mfrow=c(x,y)) 生成的图的每个面板上的标题键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆