Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mean in Scatterplot not illustrated

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading