除形状外颜色不同的线性回归线

linear regression lines with different colours other than shapes

我有一个包含 20 个点的数据集,分为 2 组(每组 10 个),在散点图中它们的颜色相同,但形状是封闭的和开放的。但是我想让 2 条回归线具有不同的颜色和线条粗细。我找到了很多 info 但不知何故无法让它工作。

数据:

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)) 

这行得通,但是有 2 条相同颜色的线条和相同的粗细

my.formula <-  y ~ x 

LM_plot <- ggplot(df, aes(x=Method1, y=Method2, color=as.character(Group), shape=as.character(Group)))+
  geom_point(aes(size=as.character(Group))) + 
  geom_smooth(method="lm", se = F, fullrange = T) +
  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)

我认为这是解决方案,但出现错误:

check_aesthetics() 中的错误: !美学必须是长度 1 或与数据相同 (20):x 和 y 运行rlang::last_error()查看错误发生的地方。

    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)

您可以在 aes 之外指定颜色,或者,如果您想将不同的比例映射到相同的美学,请使用 ggnewscale(见下文)。

您可以对您的尺码审美做同样的事情。

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'

相同美学的两个尺度

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'