retodomax
retodomax

Reputation: 625

set lwd of axis in par() function

I want to plot with a certain line width (about 1/5 of default lwd). All lines in the plot should have this 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.

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

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

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.

Upvotes: 3

Views: 1499

Answers (1)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

If we look at the formals of axis() function - default lwd is specified as 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.

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:

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

enter image description here

Upvotes: 2

Related Questions