我正在尝试在 R (ggplot2) 中重新创建一个特定的情节
I'm trying to recreate a specific plot in R (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))
这是数据库 (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))
我也尝试了所有这些多项式模型(和其他模型),但 none 有效:
## 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)
那么,我应该do/not 怎么做才能让我的台词和原来的台词一样呢?谢谢。
不要在 geom_smooth
函数中使用 method = "lm"
,而是使用具有二项式族的 glm
。 glm
-smooth 只为您提供 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()
数据
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))
尽管尝试了多种类型的线,但我还是无法得到相同的结果。 这是我需要的线条:
这就是我到目前为止的方式(并且停留在):
这是我的代码:
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))
这是数据库 (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))
我也尝试了所有这些多项式模型(和其他模型),但 none 有效:
## 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)
那么,我应该do/not 怎么做才能让我的台词和原来的台词一样呢?谢谢。
不要在 geom_smooth
函数中使用 method = "lm"
,而是使用具有二项式族的 glm
。 glm
-smooth 只为您提供 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()
数据
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))