【问题标题】:How to fit a regression line through a specific range of values and not all data on ggscatter?如何通过特定范围的值而不是ggscatter上的所有数据拟合回归线?
【发布时间】:2020-12-08 11:38:26
【问题描述】:

我使用以下代码创建了时间(x 轴)和 HDL 胆固醇(y 轴)之间关系的散点图。数据按性别着色,我想仅通过 x 轴上的特定值范围拟合回归线。我的时间数据范围从 20 到 180 天,但我只想拟合一条 30-90 天的回归线,同时仍显示绘图上的所有数据。我应该如何修改代码来实现这一点?

                    add = "reg.line",                        
                    conf.int = TRUE,                          
                    color = "Sex", alpha=0.5)+     
  stat_cor(aes(color= Sex),method = "spearman")+
  scale_color_manual(values = c("#9c0d4b", "#7ca0d4"))+
  scale_x_continuous(breaks = seq(0, 80, by = 10))+
  scale_y_continuous(breaks=seq(0, 10,2), limits=c(0,10))+
  geom_hline(yintercept=1.37, linetype="dotted", color = "black")+
  xlab("Time (days)") + ylab("HDL")+
  theme(legend.position = "none")+
  theme(text = element_text(size = 11))``` 

Thanks in advance! 

【问题讨论】:

    标签: r ggplot2 linear-regression scatter-plot lm


    【解决方案1】:

    使用可以在图中使用基本 R subset 函数:

    subset(mtcars, mpg <= 26 & mpg >= 22)
                   mpg cyl  disp hp drat   wt  qsec vs am gear carb
    Datsun 710    22.8   4 108.0 93 3.85 2.32 18.61  1  1    4    1
    Merc 240D     24.4   4 146.7 62 3.69 3.19 20.00  1  0    4    2
    Merc 230      22.8   4 140.8 95 3.92 3.15 22.90  1  0    4    2
    Porsche 914-2 26.0   4 120.3 91 4.43 2.14 16.70  0  1    5    2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-23
      • 2018-08-21
      • 2020-03-13
      • 1970-01-01
      • 2021-04-07
      • 2017-08-20
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多