How to make the trend-line in a scatter plot respect the boundaries of the x-axis?

387 views Asked by At

I am creating a plot where I plot the variable on the X-axis against that on the Y-axis, and I am adding histograms of the variables as well. I have added a trend-line to the plot using abline().

The problem is that it does not appear to respect the xlim = c(0, 20) in the plot region as it extends beyond the limits of the x-axis. I tried playing around with the xpd option, but to no avail. Next I tried fiddling with the different par()options, but found nothing that could help with this issue.

What I want is for the trend-line to be the exact length of the x-axis. Any help is much appreciated. In this particular case the trend-line is almost flat, but the slope will change when I do the same for other variables.

Graph with undesirable output

MWE -- NOTE: I am only providing 15 data points to illustrate the issue so the graph will differ from the image provided.

df.data <- data.frame(id = 1:15,
                      ll = c(-9.53026, -6.50640,-6.50640, -7.68535, -11.80899, -8.42790,
                             -6.50640, -6.50640, -7.92405, -6.50640, -8.95522, -9.99228,
                             -10.02286, -8.95969, -6.07313),
                      aspm = c(4.582104, 0.490244, 0.737765, 0.256699, 1.575931, 1.062693,
                               1.006984, 0.590355, 1.014370, 0.924855, 0.735989, 0.831025,
                               1.197886, 1.143220, 0.928068))

str.col.light.blue <- c(rgb(r = 110/255, g = 155/255, b = 225/255))
str.col.dark.blue <- c(rgb(r = 50/255, g = 100/255, b = 185/255))

layout(matrix(c(2, 4, 1, 3), 2, 2, byrow = TRUE), widths = c(5, 2), heights = c(2, 5))
layout.show(4)

par(omi = c(0.1, 0.1, 0.1, 0.1))
par(mar = c(2, 2, 0, 0))
par(mai = c(1, 1, 0, 0))

plot(df.data[, "ll"] ~ df.data[, "aspm"], col  = str.col.light.blue,
    xlim = c(0, 20), ylim = c(-15, -5), axes = FALSE,
    xlab = "X1", ylab = "X2",
    cex.lab = 1.25)

abline(a = -8.156670, b = -0.000879, lty = 5, col = "black", lwd = 2, xpd = FALSE)

axis(1, at = seq(0, 20, by = 5), labels = seq(0, 20, by = 5), cex.axis = 1)
axis(2, at = seq(-15, -5, by = 3), labels = seq(-15, -5, by = 3), cex.axis = 1, las = 1)

rect(0, -15, 20, log(1/3)*8, density = 10, angle = 45, lwd = 0.5, col = "gray")

par(mar = c(0, 2, 0, 0))
par(mai = c(0, 1, 0.25, 0))

x.hist <- hist(df.data[, "aspm"], plot = FALSE, breaks = 20)
barplot(x.hist$density, axes = FALSE, horiz = FALSE, space = 0, col = str.col.dark.blue)

par(mar = c(2, 0, 0, 0))
par(mai = c(1, 0, 0, 0.25))

y.hist <- hist(df.data[, "ll"], plot = FALSE, breaks = 20) 
barplot(y.hist$density, axes = FALSE, horiz = TRUE, space = 0, col = str.col.dark.blue)
1

There are 1 answers

1
Rui Barradas On BEST ANSWER

In order to avoid working out the start and end points of the segments, you can program a helper function to do it for you.

linear <- function(x, a, b) a + b*x

Then, I've used your code with the following changes. abline was replaced by segments, with all the graphics parameters you had used in your original call.

x0 <- 0
y0 <- linear(x0, a = -8.156670, b = -0.000879)
x1 <- 20
y1 <- linear(x1, a = -8.156670, b = -0.000879)

segments(x0, y0, x1, y1, lty = 5, col = "black", lwd = 2, xpd = FALSE)

This call to segment was placed where ablinewas.
In the final graph, I see a well behaved segment.