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

标签 r plot

我想用特定的线宽(默认 lwd 的大约 1/5)进行绘图。绘图中的所有行都应具有此 lwd

我愿意:

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

除轴线和刻度的粗细外,结果还可以,这似乎不受 par() 函数的影响。

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

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

plot without and with separate lwd setting for axis

但是,这很乏味,我无法想象没有“全局”lwd 选项。如果您知道如何针对多个绘图有效地完成此操作,请回复。

最佳答案

如果我们查看 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, ...)
{

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

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

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

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

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

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")

enter image description here

关于r - 在 par() 函数中设置轴的 lwd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50157145/

相关文章:

python - 设置matplotlibs linestyle的空间大小 "dashed"

r - 在 R 中创建空间集群 LISA map

r - 将打印输出捕获为向量

r - 如何读取以下格式的文件

r - 日期格式的转换 %B %Y

r - 在 R 中绘制数据;错误 : default method not implemented for type 'list'

python - 绘制均值和标准差

r - 如何在 r 中命名 "row names"列

R 将表连接成字符串

c++ - QCustomPlot 小部件上类似工具提示的方 block