0

我正在尝试将形状添加到回归模型中。这是示例:

library(ggpubr)
data(iris)
iris$ran <- as.factor(rep(c(1:2), each = 75))
fit <- lm(Sepal.Length ~ Petal.Width+Species+ran, data = iris)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1], 
color=names(fit$model)[3], shape=names(fit$model)[4])) +
geom_point() +
geom_smooth(aes_string(fill = names(fit$model)[3], color = names(fit$model)[3]), 
method = "lm", col= "red", fullrange = TRUE) +
labs(x=expression(paste("Petal Width")),
     y=expression(paste("Sepal Length")),
     caption = paste("R2 =",signif(summary(fit)$r.squared, 2),
                     "\tAdj R2 =",signif(summary(fit)$adj.r.squared, 2),
                     "\tIntercept =",signif(fit$coef[[1]],2 ),
                     "\tSlope =",signif(fit$coef[[2]], 2),
                     "\tP =",signif(summary(fit)$coef[2,4], 2)))+
theme_classic2(base_size = 14)

对于每个因素,我得到一个带有四条线性线的图。我宁愿只希望“物种”的线性回归线,但“跑”的不同形状(不向图中添加“跑”的回归线)。

此外,我还打算将“R2”更改为 R^2,这是我无法使用当前脚本执行的操作,并将运行的图例更改为“随机”-“Factor1”和“Factor2”。

预先感谢您的帮助。

4

2 回答 2

2

您可以scale_shape_manual根据此图表使用修改形状符号。此外,您可以直接使用 unicode 字符 ² 通过从此处复制系数来打印系数:

library(ggpubr)
#> Loading required package: ggplot2
library(tidyverse)
library(latex2exp)

iris$ran <- as.factor(rep(c(1:2), each = 75))
fit <- lm(Sepal.Length ~ Petal.Width + Species + ran, data = iris)

fit$model %>%
  ggplot(aes_string(
    x = names(fit$model)[2], y = names(fit$model)[1]
  )) +
  geom_point(aes_string(shape = names(fit$model)[4]), size = 2.5) +
  geom_smooth(aes_string(color = names(fit$model)[3]),
    method = "lm", fullrange = TRUE, se = FALSE
  ) +
  theme_classic2(base_size = 14) +
  scale_shape_manual(values = c(17, 18)) +
  labs(
    x = "Petal Width",
    y = "Sepal Length",
    caption = paste(
      "R² =", signif(summary(fit)$r.squared, 2),
      "\tAdj R²=", signif(summary(fit)$adj.r.squared, 2),
      "\tIntercept =", signif(fit$coef[[1]], 2),
      "\tSlope =", signif(fit$coef[[2]], 2),
      "\tP =", signif(summary(fit)$coef[2, 4], 2)
    )
  )
#> `geom_smooth()` using formula 'y ~ x'

reprex 包于 2021-09-09 创建(v2.0.1)

为了澄清回归线,我设置se = FALSEgeom_smooth.

于 2021-09-09T14:33:24.727 回答
1

我认为这个替代答案更简单。使用这种方法也可以使用fullrange = TRUEse = FALSE不着色点,但这会产生严重歪曲数据的图。即使这不会产生相同的标题,我的答案中的代码也会自动显示三个拟合中的每一个的结果,并且它会在不同数量的因子水平下保持不变。

这里iris使用数据作为示例,因此宽度和长度都是随机变量,可以忽略并使用 OLS。否则主轴回归会更可取,并且可以使用并稍微调整传递给它们的参数来重写下面的stat_ma_line()代码stat_ma_eq()

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
iris$ran <- factor(rep(c(1:2), each = 75), labels = paste("Factor", 1:2))

ggplot(iris, aes(Petal.Width, Sepal.Length, colour = Species)) +
  geom_point(aes(shape = ran)) +
  stat_poly_line() + # se = FALSE can be added
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
#                                 after_stat(adj.rr.label),
                                 after_stat(eq.label), 
                                 after_stat(p.value.label),
#                                 after_stat(n.label),
                                 sep = "*\", \"*"))) +
  labs(x = "Petal Width", y = "Sepal length", shape = "Random") +
  theme_classic(base_size = 14)

reprex 包于 2021-09-10 创建(v2.0.1)

于 2021-09-10T17:29:54.050 回答