Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the significant figures on R-squared in stat_regline_equation()?

Tags:

r

ggplot2

ggpubr

Here is a sample of my data:

df <- data.frame(surface_area = c(0.25, 1, 2.25, 4, 6.25),
             weight = c(0.8, 3.267, 7.733, 13.533, 21.8333))

The code for my plot and my plot:

    plot <- ggplot(data = df, aes(x = surface_area, y = weight)) + geom_point() +
  geom_smooth(method = 'lm',
              se = FALSE) +
  stat_regline_equation(aes(label = ..rr.label..),
                        label.x = 4, 
                        label.y = 5.8, 
                        size = 6) +
  stat_regline_equation(label.x = 4, 
                        label.y = 6.5, 
                        size = 6)

enter image description here

The line doesn't quite perfectly fit the data, but I am still getting a R-squared of 1. How do I increase the significant figures or decimal places of the R-squared to get a more accurate R-squared?

Many thanks!

like image 525
dan_confused Avatar asked Feb 02 '26 17:02

dan_confused


1 Answers

Another option may be by first calculating the coefficient using lm with summary and after that change the coefficient that is plotted with gsub using ggplot_build to change the label of the layer that plots the coefficient. Here is some reproducible code:

library(ggplot2)
library(ggpubr)
model = lm(weight~surface_area, data = df)
r_squared = summary(model)$r.squared
r_squared
#> [1] 0.9995947
p = ggplot(data = df, aes(x = surface_area, y = weight)) + geom_point() +
  geom_smooth(method = 'lm',
              se = FALSE) +
  stat_regline_equation(aes(label = ..rr.label..),
                        label.x = 4, 
                        label.y = 5.8, 
                        size = 6) +
  stat_regline_equation(label.x = 4, 
                        label.y = 6.5, 
                        size = 6)

q <- ggplot_build(p)
q$data[[3]]$label = gsub("1", as.character(r_squared), q$data[[3]]$label)
q <- ggplot_gtable(q)
plot(q)

Created on 2023-03-24 with reprex v2.0.2


If you want to round your coefficient you could use the round function on the calculate coefficient like this:

library(ggplot2)
library(ggpubr)
model = lm(weight~surface_area, data = df)
r_squared = summary(model)$r.squared
p = ggplot(data = df, aes(x = surface_area, y = weight)) + geom_point() +
  geom_smooth(method = 'lm',
              se = FALSE) +
  stat_regline_equation(aes(label = ..rr.label..),
                        label.x = 4, 
                        label.y = 5.8, 
                        size = 6) +
  stat_regline_equation(label.x = 4, 
                        label.y = 6.5, 
                        size = 6)

q <- ggplot_build(p)
q$data[[3]]$label = gsub("1", as.character(round(r_squared, 4)), q$data[[3]]$label)
q <- ggplot_gtable(q)
plot(q)

Created on 2023-03-24 with reprex v2.0.2

like image 156
Quinten Avatar answered Feb 05 '26 07:02

Quinten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!