在par()函数中设置轴的lwd

时间:2021-03-10 23:41:11

I want to plot with a certain line width (about 1/5 of default lwd). All lines in the plot should have this lwd.

我想绘制一定的线宽(约为默认lwd的1/5)。图中的所有线都应该有这个lwd。

I do:

我做:

par(lwd = 0.2)
plot(1:10, type = "l")

The result is ok except for the thickness of the axes lines and ticks, which seems to be unaffected by the par() function.

结果没有问题,除了轴线和刻度线的厚度,这似乎不受par()函数的影响。

The only option I know is to separately define the lwd for each axis:

我知道的唯一选择是为每个轴单独定义lwd:

par(lwd = 0.2)
plot(1:10, type = "l", axes = F)
axis(1, lwd = 0.2)
axis(2, lwd = 0.2)
box()

在par()函数中设置轴的lwd

However, this is tedious and I can not imagine that there is no "global" lwd option. If you have an idea how this could be done efficiently for several plots please respond.

然而,这是乏味的,我无法想象没有“全局”lwd选项。如果你知道如何有效地为几个地块做到这一点,请回复。

1 个解决方案

#1


1  

If we look at the formals of axis() function - default lwd is specified as 1:

如果我们查看axis()函数的形式 - 默认lwd指定为1:

> axis
function (side, at = NULL, labels = TRUE, tick = TRUE, line = NA,
    pos = NA, outer = FALSE, font = NA, lty = "solid", lwd = 1,
    lwd.ticks = lwd, col = NULL, col.ticks = NULL, hadj = NA,
    padj = NA, ...)
{

And as you noticed they are not affected by the par() setting in this implementation.

正如您所注意到的,它们不受此实现中par()设置的影响。

One simple solution would be to make a wrapper for axis() function and make it use the par() setting by default. Here is how that might look like:

一个简单的解决方案是为axis()函数创建一个包装器,并使其默认使用par()设置。这可能是这样的:

axislwd <- function(...) axis(lwd=par()$lwd, ...)

par(lwd = 0.2)
plot(1:10, type = "l", axes = F)
axislwd(1)
axislwd(2)
box()

Alternatively you can write a wrapper for the whole plot function instead:

或者,您可以为整个绘图函数编写一个包装器:

plotlwd <- function(...) {
  plot(axes = FALSE, ...)
  axis(1, lwd=par()$lwd)
  axis(2, lwd=par()$lwd)
  box()
}

par(lwd = 0.2)
plotlwd(1:10, type="l")

在par()函数中设置轴的lwd

#1


1  

If we look at the formals of axis() function - default lwd is specified as 1:

如果我们查看axis()函数的形式 - 默认lwd指定为1:

> axis
function (side, at = NULL, labels = TRUE, tick = TRUE, line = NA,
    pos = NA, outer = FALSE, font = NA, lty = "solid", lwd = 1,
    lwd.ticks = lwd, col = NULL, col.ticks = NULL, hadj = NA,
    padj = NA, ...)
{

And as you noticed they are not affected by the par() setting in this implementation.

正如您所注意到的,它们不受此实现中par()设置的影响。

One simple solution would be to make a wrapper for axis() function and make it use the par() setting by default. Here is how that might look like:

一个简单的解决方案是为axis()函数创建一个包装器,并使其默认使用par()设置。这可能是这样的:

axislwd <- function(...) axis(lwd=par()$lwd, ...)

par(lwd = 0.2)
plot(1:10, type = "l", axes = F)
axislwd(1)
axislwd(2)
box()

Alternatively you can write a wrapper for the whole plot function instead:

或者,您可以为整个绘图函数编写一个包装器:

plotlwd <- function(...) {
  plot(axes = FALSE, ...)
  axis(1, lwd=par()$lwd)
  axis(2, lwd=par()$lwd)
  box()
}

par(lwd = 0.2)
plotlwd(1:10, type="l")

在par()函数中设置轴的lwd