stat_poly_eq (change the position of the equation)

Hi,

If you look at the plot, the regression equation is in the format of y = c +mx, I want to change it in y = mx +c . for example, 1.06+2.25x to in the format of 2.25x+1.06

The code i am using for this plot is

AE33_NCORE_hourly %>% 
  ggplot(aes(y = (AE33), x=PAX, color="blue"))  +
  geom_point(size=2,alpha=0.9, color="blue", alpha = 0.3, shape = 1) +  geom_smooth(method=lm, size=0.3) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") + # Add 1:1 line
  labs(title= "") +
  xlim(0,40)+   
  ylim(0,100)+ 
  theme(legend.text=element_text(size=14)) + 
  theme(axis.title = element_text(face="plain",size=14,color="black"),axis.text=element_text(size=14,face="plain", color="black")) +
  theme(plot.title = element_text(lineheight=.8, face="plain", size=14)) +
  theme(legend.key.height = unit(0.4, "inch"))+ 
  theme(legend.position="")+
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label.., ..rr.label.., ..p.value.label.., sep = "*`,`~")), 
               parse = TRUE,
               label.x.npc = "left",
               vstep = 0.07, size=3.8) 

Thanks

Hi,
Maybe a little bit tedious, but worked when i tested it. My basic idea was to extract the eq.label formula, split it into parts, and reorder them:

AE33_NCORE_hourly %>% 
  ggplot(aes(y = (AE33), x=PAX, color="blue"))  +
  geom_point(size=2,alpha=0.9, color="blue", alpha = 0.3, shape = 1) +  geom_smooth(method=lm, size=0.3) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") + # Add 1:1 line
  labs(title= "") +
  xlim(0,40)+   
  ylim(0,100)+ 
  theme(legend.text=element_text(size=14)) + 
  theme(axis.title = element_text(face="plain",size=14,color="black"),axis.text=element_text(size=14,face="plain", color="black")) +
  theme(plot.title = element_text(lineheight=.8, face="plain", size=14)) +
  theme(legend.key.height = unit(0.4, "inch"))+ 
  theme(legend.position="")+
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste({
                  eq <- ..eq.label..
                  
                  # Step 1: Split the equation at '='
                  eq_split <- str_split(eq, "~`=`~")[[1]]
                  
                  # eq_split[1] is "italic(y)"
                  # eq_split[2] is the right-hand side: "72.4 - 1.70*~italic(x)"
                  
                  # Step 2: Split the right-hand side at "+" or "-" to separate intercept and slope
                  eq_rhs_split <- str_split(eq_split[2], "(\\+|\\-)")[[1]]
                  
                  # eq_rhs_split[1] is the intercept ("72.4")
                  # eq_rhs_split[2] is the slope part ("1.70*~italic(x)")
                  
                  # Step 3: Get the sign (+ or -) from the original right-hand side of the equation
                  sign <- str_extract(eq_split[2], "(\\+|\\-)")
                  
                  # Step 4: Reorder the parts
                  new_eq <- paste(eq_split[1],  # "italic(y)"
                                  "~`=`~", 
                                  eq_rhs_split[2],  # slope (e.g., "1.70*~italic(x)")
                                  sign,  # the sign (+ or -)
                                  eq_rhs_split[1])  # intercept (e.g., "72.4")
                  
                  # Output the new equation
                  new_eq
        } ..rr.label.., sep = "*`,`~") ), 
               parse = TRUE,
               label.x.npc = "left",
               vstep = 0.07, size=3.8) 
2 Likes