user3616494 user3616494 - 27 days ago 6x
R Question

How to draw straight line cross center of data in scatter plot?

I wanted to draw a scatter plot with ggplot which

cross center of my data with 45 degree angle not drawing automatically.

As it shows here the geom_smooth() has small slop and I changed different method lm, auto and etc, but there was no difference.

geom_smooth(color = "black", alpha = 0.5, method = "lm", se = F)

How can I draw line in way exactly cross middle of the pink dots?

enter image description here


Do you want something like this?

geom_abline(intercept = 0, slope = 1)

This wll draw a line with a 45ยบ angle that goes through 0.

I think geom_smooth is computing the regression fr all the points. Try to remove all color argument to get the smooth by group. You can check the different plots

a <- data.frame(x = c(1:10,2:11),y = c(2:11,1:10), label = c(rep("a",10),rep("b",10)))

ggplot(a, aes(x,y)) + geom_point(aes(color = label)) + geom_smooth(aes(color = label))
ggplot(a, aes(x,y)) + geom_point(aes(color = label)) + geom_smooth(color="black")