Home > Software engineering >  Mean in Scatterplot not illustrated
Mean in Scatterplot not illustrated

Time:10-30

I want to assign the means of x and y to a scatterplot in R without ggplot, just basic R. However, in the way I try (using abline), it does not illustrate the lines in the plot. I already had to do another task earlier, hence the slightly longer code.

I tried using abline for either x and y to assign a horizontal and a vertical dashed line to the plot. The result was that the plot inclusive the blue dot is illustrated, but the means don't show up.

plot(x, y, axes = T, pch = 16, xlim = c(40,120), points(46, 150, col = "blue", pch = 16), main = abline(v = mean(y), lty = 2, lwd = 4) & abline(h = mean(x), lty = 2, lwd = 4))

CodePudding user response:

You have a few syntax errors in the supplied code, but ultimately I think your problem is that you are trying to plot a vertical line at the mean y value when it should be at the mean x value, and a horizontal line at the mean x value, when it should be at the mean y value:

set.seed(1)

x <- rnorm(10, 47, 5)
y <- rnorm(10, 150, 5)

plot(x, y, axes = T, pch = 16)
points(46, 150, col = "blue", pch = 16)
abline(v = mean(x), lty = 2, lwd = 4)
abline(h = mean(y), lty = 2, lwd = 4)

Created on 2022-10-30 with reprex v2.0.2

  • Related