I have a dataset df. I would like to make two groups, containing a= Special and b=Up+ Down+ Left. Then I would like to have 2x determination coefficient and 2 regression lines for each subgroup in one plot. Problem is how to call these created subsets and not overplot my R2. With this code, I make a line for each single group.
library(tidyverse)
set.seed(2013)
Place <- sample(c("Up", "Down", "Left", "Special"), 100, replace = TRUE)
Value <- sample(200:1500, 100, replace = TRUE)
Conc <- sample(1:25, 100, replace = TRUE)
df <- tibble(Place, Value, Conc)
#make subgroup only Special
a <- df |>
filter(Place == "Special")
#make subgroup Up+Down+Left
U <-"Up"
D <-"Down"
L <-"Left"
Subgroup <- c(U,D,L)
b <- df |>
filter(Place %in% Subgroup)
#Scatter and Regression for all Place seperated
df |>
ggplot(aes(Value, Conc))+
geom_point(aes(col = Place))+
stat_poly_line(aes(col = Place), se = FALSE)+
stat_poly_eq()
You haven't specified a grouping variable for the stat_poly_eq
layer. In the geom_point
layer and stat_poly_line
layer, you have mapped the "colour" aesthetic to the Place
column, but you haven't done this for stat_poly_eq
, so a single R2 value is produced. You can specify the group using a simple ifelse
to get groups "a" and "b".
df |>
ggplot(aes(Value, Conc, colour = Place)) +
geom_point() +
stat_poly_line(se = FALSE) +
stat_poly_eq(aes(group = ifelse(Place == "Special", "group~a", "group~b"),
blah = ifelse(Place == "Special", "group~a", "group~b"),
label = paste0(after_stat(blah), ": ",
after_stat(rr.label))),
colour = "black") +
theme_bw()