This is probably very trivial and I can't get my head around this.
Note: all the codes below are in R
I have the following x and y values:
y_values <- c(68.5626444935181, 74.9499148487446,
79.8497409367012, 73.0839198881826, 69.3704162253844, 71.2955559142127,
73.3039295493942, 80.4815231294542, 77.3386138482087, 76.7735812401891,
83.1127314163547, 76.0379064535433, 76.3375388691764, 79.3073079200627,
79.4969630108931, 75.4074011408933, 73.1746588455723, 74.7381647481472,
76.0068878713895, 66.6933533294857, 77.2538702691301, 72.3045360297129,
80.9516081348814, 81.5990676377598, 73.3101299846443, 74.542113081284,
78.0547705030883, 70.7484704958974, 74.0914515886154, 70.0722061819694,
77.1576715278398, 76.340436178083, 72.345917299701, 73.8302355573314,
74.5860765982042, 72.0199091680495, 71.6603668197653, 76.2407368365496,
73.085653645391, 71.6614849907091, 77.5958284776863, 69.2870233168088,
91.4953861502152, 78.1859047973624, 72.2066485610847, 75.7656423615475,
71.2736144202631, 73.8804703782756, 73.806815498892, 72.3512968378224
)
x_values <- c(66.714969241285, 74.6001367053999, 82.488038277512,
79.9945317840055, 65.6650717703349, 74.930963773069, 82.2255639097745,
84.7272727272727, 80.1667805878332, 81.1237183868763, 83.1414900888585,
76.6124401913876, 82.2802460697198, 84.6507177033493, 85.0061517429939,
84.4538619275461, 82.6110731373889, 79.6144907723855, 80.2816131237184,
66.9665071770335, 79.1032125768968, 73.8427887901572, 80.6643882433356,
79.4231032125769, 79.2809295967191, 89.4545454545455, 81.0635680109364,
77.0307587149692, 77.4545454545455, 75.8441558441558, 77.7908407382092,
78.4333561175666, 76.9405331510595, 76.9596719070403, 76.3991797676008,
75.3246753246753, 73.6623376623377, 76.7081339712919, 74.9801777170198,
71.2071086807929, 81.1811346548189, 65.3807245386193, 85.4654818865345,
80.0328092959672, 65.161995898838, 81.9302802460697, 73.5037593984962,
77.367053998633, 75.5652768284347, 72.6015037593985)
d <- data.frame(y_values=y_values, x_values=x_values)
If I plot y vs x this is what I have:
library(ggpmisc)
ggplot(d, aes(y = y_values, x = x_values)) +
geom_smooth(method = "lm", se = FALSE, color = "lightgrey", formula = y ~ x) + geom_point( alpha=0.3) +
stat_poly_eq(aes(label = paste0("atop(", ..eq.label.., ",", ..rr.label.., ")")),
formula = y ~ x,
parse = TRUE)
I used the stat_poly_eq function from ggpmisc to get the line's equation
Now I want to transform y so that y == x and all the points are moved on the diagonal. I tried to extract the coefficients from the equation and transform y values like so:
transform_y <- function(raw_y, a=0.548,b=32.5){
return(a*(raw_y-b))
}
d <- d %>%
mutate(y_values_new = transform_y(y_values) )
The new plot looks like this:
ggplot(d, aes(y = y_values_new, x = x_values)) +
geom_smooth(method = "lm", se = FALSE, color = "lightgrey", formula = y ~ x) + geom_point( alpha=0.3) +
stat_poly_eq(aes(label = paste0("atop(", ..eq.label.., ",", ..rr.label.., ")")),
formula = y ~ x,
parse = TRUE)
All the points are aligned but not on the y=x diagonal.
What am I doing wrong here ?