我正在尝试在R中重新创建一个特定的图(ggplot2)

Ben*_*min 4 r lines spline ggplot2

尽管已尝试过多种类型的线,但我无法得到相同的结果.以下是我需要线条的方式:

原始情节

这就是我到目前为止得到它的方式(我坚持): 我的情节版本

这是我的代码:

myData <- read.csv(file.choose(), header = TRUE)
require(ggplot2)
g <- ggplot(myData, aes(speed, resp))
g + geom_point(aes(color = padlen, shape = padlen)) +
geom_smooth(method = "lm", formula = y ~ splines::bs(x, df = 4, degree = 2), se = FALSE, aes(color = padlen), linetype = "solid", size = 1) +
scale_color_manual(values = c("red", "black")) +
scale_shape_manual(values = c(2, 1))
Run Code Online (Sandbox Code Playgroud)

这是数据库(dput):

myData <- structure(list(resp = c(0, 0.125, 0.583333333, 1, 0.958333333, 
1, 0, 0.041666667, 0.25, 0.916666667, 1, 1), padlen = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("big", 
"small"), class = "factor"), speed = c(2L, 3L, 4L, 5L, 6L, 7L, 
2L, 3L, 4L, 5L, 6L, 7L)), .Names = c("resp", "padlen", "speed"
), class = "data.frame", row.names = c(NA, -12L))
Run Code Online (Sandbox Code Playgroud)

我也尝试了所有这些多项式模型(和其他),但没有一个工作:

## Quadratic model
lmQuadratic <- lm(formula = y ~ x + I(x^2),
                  data    = fpeg)
## Cubit model
lmCubic <- lm(formula = y ~ x + I(x^2) + I(x^3),
              data    = fpeg)
## Fractional polynomial model
lmFractional <- lm(formula = y ~ x + I(x^2) + I(x^(1/2)),
                   data    = fpeg)
Run Code Online (Sandbox Code Playgroud)

那么,我应该做什么/不做什么来让我的线条与原来的线条相同?谢谢.

kat*_*ath 7

而不是method = "lm"geom_smooth函数中使用glm与二项式家族一起使用.该glm平滑肌使您只有0和1之间的值(你想拥有什么,因为你与比例处理).

library(ggplot2)

ggplot(myData, aes(speed, resp)) + 
  geom_point(aes(color = padlen, shape = padlen)) +
  geom_smooth(method = "glm", method.args = list(family = "binomial"), 
              se = FALSE, aes(color = padlen), linetype = "solid", size = 1) +
  scale_color_manual(values = c("red", "black")) +
  scale_shape_manual(values = c(2, 1)) +
  theme_classic()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

数据

myData <- 
  structure(list(resp = c(0, 0.125, 0.583333333, 1, 0.958333333, 1, 0, 
                          0.041666667, 0.25, 0.916666667, 1, 1), 
                 padlen = c("small", "small", "small", "small", "small", 
                            "small", "big", "big", "big", "big", "big", "big"), 
                 speed = c(2L, 3L, 4L, 5L, 6L, 7L, 2L, 3L, 4L, 5L, 6L, 7L)), 
            .Names = c("resp", "padlen", "speed"), class = "data.frame", 
            row.names = c(NA, -12L))
Run Code Online (Sandbox Code Playgroud)