简体   繁体   English

R geom_smooth 自定义最佳拟合线公式

[英]R geom_smooth custom best fitted line formula

Geom_smooth formula Geom_smooth公式

I am currently trying to fit a line to my data.我目前正在尝试为我的数据拟合一条线。

However, no fitted line is shown on the plot with none of the default methods of geom_smooth.但是,在没有 geom_smooth 的默认方法的情况下,plot 上没有显示任何拟合线。

Here is the code that generated the plot:这是生成 plot 的代码:

p1 <- ggplot(data[ which(data$subject_id == "P121" & !is.na(data$col1)), ], aes(x = date, y= col1)) + 
  geom_point(colour= met.brewer("Isfahan1", 1)) + 
  geom_smooth(method = "stats::loess", se= FALSE) +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
                      axis.ticks.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(),
                      axis.ticks.y =element_blank(), legend.title=element_blank()) +
  scale_color_manual(values=met.brewer("Isfahan1", 13))

This is the plot that is created by the code above I am aware that we can customize the formula for the model to be fitted, however, I'm new to R. Could you guide me on how to decide on the formula?这是由上面的代码创建的 plot我知道我们可以自定义要拟合的 model 的公式,但是,我是 R 的新手。你能指导我如何决定公式吗?

Your problem is using the string 'stats::' in the method parameter of the geom_smooth function.您的问题是在geom_smooth function 的method参数中使用字符串'stats::'

See the code below看下面的代码

library(tidyverse)
library(MetBrewer)

i1 = c(21, 25, 27, 30:33, 35, 37, 38:41, 44, 46:48, 53, 54)
df = tibble(
  date = 1:55,
  col1 = ifelse(date %in% i1, 200, 100)
)

df %>%  ggplot(aes(date, col1)) + 
  geom_point(colour= met.brewer("Isfahan1", 1)) + 
  geom_smooth(formula = y~x, method = "loess", se= FALSE) + 
  theme(
    axis.title.x=element_blank(), 
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank(), 
    axis.title.y=element_blank(), 
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(), 
    legend.title=element_blank()) +
  scale_color_manual(values=met.brewer("Isfahan1", 13))

在此处输入图像描述

You can also use another method, eg linear您还可以使用另一种方法,例如线性

df %>%  ggplot(aes(date, col1)) + 
  geom_point(colour= met.brewer("Isfahan1", 1)) + 
  geom_smooth(formula = y~x, method = "lm", se= FALSE) + 
  theme(
    axis.title.x=element_blank(), 
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank(), 
    axis.title.y=element_blank(), 
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(), 
    legend.title=element_blank()) +
  scale_color_manual(values=met.brewer("Isfahan1", 13))

在此处输入图像描述

However, pay attention to the data you give to the ggplot function. If your date variable is not of numeric type, however, plot will be created without smooth path.但是,请注意您为ggplot function 提供的数据。如果您的date变量不是数字类型,则创建的 plot 将没有平滑路径。

df = tibble(
  date = 1:55 %>% as.character(),
  col1 = ifelse(date %in% i1, 200, 100)
)

df %>%  ggplot(aes(date, col1)) + 
  geom_point(colour= met.brewer("Isfahan1", 1)) + 
  geom_smooth(formula = y~x, method = "loess", se= FALSE) + 
  theme(
    axis.title.x=element_blank(), 
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank(), 
    axis.title.y=element_blank(), 
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(), 
    legend.title=element_blank()) +
  scale_color_manual(values=met.brewer("Isfahan1", 13))

在此处输入图像描述

As you can see in this example, I changed the value of the variable date to type character ( date = 1:55 %>% as.character() ) which made geom_smooth to not draw anything.如您在此示例中所见,我将变量date的值更改为类型character ( date = 1:55 %>% as.character() ),这使得geom_smooth不绘制任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM