0

I am plotting a dataset in R, and have set the y-axis to a log-scale.

My data splits into to trends which I have modelled, and added as trendlines to the chart. When using the simple lines() command, it creates the dashed line that is affected by the log-scale.

Plot as it looks

As can be seen from the figure, when using the simple command lines(x_1, predict(fit_1), lty = 2, lwd = 1, col = "red", log = "y"), it creates the dashed line that is impacted by the log scale.

Is there a way to set the dashed line's dashes and spaces to vary with the log-scale to prevent the dashes bunching into a solid line at the centre of the trend line?

The model is literally just a simple expression y = A*10^(mx).

This is purely a cosmetic issue for publication.

James.H
  • 25
  • 6
  • 3
    Dashed lines work just fine even with log axes in R. I guess the problem is that your *x* values are not ordered which results in some parts of the line being overdrawn many times. Does `lines(sort(x_1), predict(fit_1)[order(x_1)], lty = 2, lwd = 1, col = "red", log = "y")` look better? – Robert Hacken Mar 21 '23 at 18:02

1 Answers1

1

I admit I'm not 100% sure what's going on here since you didn't give us a reproducible example. Since these are straight lines, could you use just the endpoints?

ends <- function(z) z[c(1, length(z))]
lines(ends(x_1), ends(predict(fit_1)), lty = 2, lwd = 1, 
               col = "red", log = "y")

If as @RobertHacken suggests the problem is that the values aren't sorted, we could use

ends <- function(z) z[c(which.min(x_1), which.max(x_1))]

(note that the 'ends' of both vectors are determined by the position of the min/max of x_1)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453