我正在使用以下代码在R中创建一个boxplot:
boxplot(perc.OM.y ~ Depth, axes = F, ylim = c(-0.6, 0.2), xlim = c(3.5, 5.5),
lwd = 0.1, col = 8,
ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5)
axis(1, at = c(3.5, 4, 5, 5.5), labels = c(" ", "Shallow", "Deep", " "),
cex.axis = 1.5)
axis(2, cex.axis = 1.5)
Run Code Online (Sandbox Code Playgroud)
问题是y轴上的数字标签当前与轴标题重叠.有没有办法在轴标题和轴号标签之间放置更多空间?
谢谢
Rei*_*son 42
## dummy data
dat <- data.frame(Depth = sample(c(3:6), 20, replace = TRUE), OM = 5 * runif(20))
Run Code Online (Sandbox Code Playgroud)
通过在图(side = 2)的左侧使边距更大,为y轴标签和注释添加一些空间:
## margin for side 2 is 7 lines in size
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
Run Code Online (Sandbox Code Playgroud)
现在情节:
## draw the plot but without annotation
boxplot(OM ~ Depth, data = dat, axes = FALSE, ann = FALSE)
## add axes
axis(1, at = 1:4, labels = c(" ", "Shallow", "Deep", " "), cex.axis = 1.5)
axis(2, cex.axis = 2)
## now draw the y-axis annotation on a different line out from the plot
## using the extra margin space:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
## draw the box to finish off
box()
Run Code Online (Sandbox Code Playgroud)
然后重置绘图边距:
par(op)
Run Code Online (Sandbox Code Playgroud)
这给出了:

因此,我们为第2侧的绘图边缘创建了更多空间,然后分别绘制轴和注释(ylab)以控制绘图的间距.
所以关键是这一行:
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
Run Code Online (Sandbox Code Playgroud)
我们所做的是将原始图形参数保存在对象中op,并将边距大小(行数)分别更改为5,7,4,2 + 0.1行,分别为bottom,left,top,right margin.上面的行显示默认值,因此代码在左边距上比在默认情况下通常提供的多2行.
然后,当我们使用绘制y轴标签时title(),我们指定在(在7中)绘制标签的那一行:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
Run Code Online (Sandbox Code Playgroud)
在这里我使用了line 4.5,但5也会工作.'line'标注绘制距离图越远的值越大.
关键是要找到左边的值和值'line'在title()允许轴的刻度线和轴标签不重叠的呼叫.试验和错误可能是找到基本图形所需值的最佳解决方案.
Aar*_*ica 23
尝试设置mgp较大的第一个值.你也想把边距做得更大一些mar.
par(mgp=c(5,1,0))
par(mar=c(5,6,4,2)+0.1)
Run Code Online (Sandbox Code Playgroud)
当我想缩小图表周围的空白区域时(考虑会议论文中的大小限制!),同时我想避免重叠 Y 轴标题和大数字作为刻度,我发现这个解决方案非常简单和有用。
在手动设置边距后,我习惯将标题设置为文本并将它们放在我想要的任何位置:
首先,将边距设置为任意值:
par( mar=c(m1, m2, m3, m4) )
Run Code Online (Sandbox Code Playgroud)
其中 m1 到 m4 是四个边的边距(1=底部,2=左侧,3=顶部和 4=右侧)。
例如:
par( mar=c(3.1, 4.7, 2.3, 0))
Run Code Online (Sandbox Code Playgroud)
然后,在绘图时,设置 main=""、xlab="" 和 ylab=""(否则它们的文本将与此新文本重叠)
最后,使用 mtext(),手动设置轴标题和图表标题:
mtext(side=1, text="X axes title", line=0.5)
mtext(side=2, text="Y axes title", line=3)
mtext(side=3, text="Diagram title", line=1.5)
Run Code Online (Sandbox Code Playgroud)
线参数是与图表的距离(值越小,离图表越近)。
| 归档时间: |
|
| 查看次数: |
82782 次 |
| 最近记录: |