'How do I put a R² value on a graph with reordered axis values?

I am trying to put an R² value on a graph where I have filtered and ordered and summarised some values, and it will not let me. It allows a line of best fit but the R² value will not appear despite a stat_regline_equation line.

Here is my code:

s3average <- summarySE(S2_data, measurevar="Oxygen_consumption_rate", groupvars=c("Species", "Min", "Max", "Median", "Feeding"))

s3average$FeedingOrder <- factor(s3average$Feeding, levels= c("NA", "Large nektonic", "Mixed", "Small nektonic", "Planktonic"))

Core_feeding <- filter(s3average, grepl('Large nektonic|Small nektonic|Planktonic', Feeding))

diet2 <- ggplot(Core_feeding, aes(FeedingOrder, Oxygen_consumption_rate))+
  geom_point()+
  geom_smooth(method=lm, na.rm = TRUE, aes(group=1),colour="black", se = FALSE)+
  stat_regline_equation(aes(label = ..rr.label..))
print(diet2)

It winds up looking like this: Graph that is made with lack of R² value

Despite the line of code requesting the value it does not give it and I cannot figure out why.



Solution 1:[1]

Add group=1 to your top level aesthetics rather than in geom_smooth():

data(iris)

library(ggplot2)
library(ggpubr)

ggplot(iris, aes(x = Species, y = Sepal.Length, group = 1)) +
  geom_point() +
  geom_smooth(method = lm, na.rm = TRUE, colour = "black", se = FALSE) +
  stat_regline_equation(aes(label = ..rr.label..))

Created on 2022-03-28 by the reprex package (v2.0.1)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Skaqqs