Home > Enterprise >  Intersection of two lines in semi-logarithmic plot
Intersection of two lines in semi-logarithmic plot

Time:11-05

Two points:

df.x <- c(0.0014, 0.0027)
df.y <- c(17.38, 22.49)

I want to know y at x==0.002. Linear interpolation gives 19.74.

A linear plot shows the intersection:

plot(df.x, df.y, type="b", xlim=c(0.001,0.005), ylim=c(10,30))
abline(v=0.002)
abline(h=19.74)

However, setting the x-axis to logarithmic, the line between the two points crosses x=0.002 at y>19.74:

plot(df.x, df.y, log="x", type="b", xlim=c(0.001,0.005), ylim=c(10,30))
abline(v=0.002)
abline(h=19.74)

I have absolutely no idea about the reason of this shift in y-direction.

I played around with: untf in abline(), par(), log10()

CodePudding user response:

You haven't plotted a curve following a formula, you've plotted two points (positioned by the log scale) then connected them with a straight line. Contrast:

# your data
df <- data.frame(x = c(0.0014, 0.0027),
                 y = c(17.38, 22.49))

#interpolate 1000 points
df2 <- data.frame(x = seq(from = df$x[1],
                          to = df$x[2],
                          length.out = 1000),
                  y = seq(from = df$y[1],
                          to = df$y[2],
                          length.out = 1000))

plot(df2, log="x", type="b", xlim=c(0.001,0.005), ylim=c(10,30))
abline(v=0.002)
abline(h=19.74)

gives
enter image description here

CodePudding user response:

The reason is logarithm transformation is not a linear transformation. Therefore, you need to do the interpolation again under logarithm transformation.

approx(log(df.x),df.y,xout=log(0.002))
$x
[1] -6.214608

$y
[1] 20.15507

plot:

plot(df.x, df.y, log="x", type="b", xlim=c(0.001,0.005), ylim=c(10,30))
abline(v=0.002)
abline(h=20.15507)
  • Related