Home > database >  linear regression lines with different colours other than shapes
linear regression lines with different colours other than shapes

Time:06-01

I have a dataset of 20 points which is grouped in 2 groups (10 each) where in the scatterplot they are coloured the same, but shape is closed and open. However I would like to have the 2 regression lines a different colour and line thickness. Ive found a lot of enter image description here

I thought this was the solution but get an error:

Error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (20): x and y Run rlang::last_error() to see where the error occurred.

    my.formula <-  y ~ x 

LM_plot <- ggplot(df, aes(x=Method1, y=Method2, color=Group, shape=Group)) 
  geom_point(aes(size=Group), inherit.aes=T)   
  #geom_smooth(method="lm", se = F, fullrange = T)  
  geom_smooth(aes(x=df$Method1[1:10], y=df$Method2[1:10]), method="lm", se=T, inherit.aes=F, colour="blue", size=4, fullrange = T, formula = my.formula)  
  geom_smooth(aes(x=df$Method1[11:20], y=df$Method2[11:20]), method="lm", se=T, inherit.aes=F, colour="blue", size=2, fullrange = T, formula = my.formula)  
  scale_shape_manual(values = c(18, 5))  
  scale_color_manual(values = c("#66ccfe", "#66ccfe"))  
  scale_size_manual(values = c(5, 5))  
  scale_x_continuous("Method1")  
  scale_y_continuous("Method2")  
  stat_poly_eq(aes(label = paste0("atop(", ..eq.label.., ",", ..rr.label.., ")")), 
               formula = my.formula, 
               parse = TRUE, size = 8)

LM_plot <- LM_plot   theme_prism(base_size = 20)

CodePudding user response:

You can specify color outside of aes, or, if you want to map different scales to the same aesthetic, use ggnewscale (see below).

You can do the same with your size aesthetic.

library(ggplot2)
df <- data.frame("Group" = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
                 "Subject" = c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
                 "Method1" = c(2,5,6,8,9,10,15,20,30,40,3,5,6,8,11,14,18,90,22,23),
                 "Method2" = c(1,2,3,4,5,6,7,8,9,10,23,33,35,40,45,60,80,90,100,111)) 
my.formula <-  y ~ x 

ggplot(df, aes(x=Method1, y=Method2, shape=as.character(Group))) 
  ## use color outside aes
  geom_point(aes(size=as.character(Group)), color = "#66ccfe")   
  ## add size outside of aes
  geom_smooth(method="lm", se = F, fullrange = T, color = "darkblue", size = 2)  
  scale_shape_manual(values = c(18, 5))  
  scale_size_manual(values = c(5, 5)) 
#> `geom_smooth()` using formula 'y ~ x'

two scales for the same aesthetic

ggplot(df, aes(x=Method1, y=Method2, shape=as.character(Group))) 
  # use color outside aes
  geom_point(aes(size=as.character(Group), color = I("#66ccfe")))  
  scale_shape_manual(values = c(18, 5))  
  scale_size_manual(values = c(5, 5))  
  ggnewscale::new_scale_color()  
  ggnewscale::new_scale("size")  
  # now add color into aes
  geom_smooth(aes(color = as.character(Group), size = as.character(Group)),
              method="lm", se = F, fullrange = T)  
  scale_color_brewer(palette = "Set1")  
  scale_size_manual(values = c(1:2))
#> `geom_smooth()` using formula 'y ~ x'

  • Related