非线性拟合 ggplot2 R 中的 facet_grid 函数
Non-linear fits in facet_grid function in ggplot2 R
对于 X 列,所有图都需要拟合线性回归,而对于 Y 列,则需要线性和二次的混合。但是,在图中,所有拟合都是二次的。在某些情况下也没有拟合线。对调整代码有什么建议吗?
set.seed(14)
df <- data.frame(
var.test = rep(c("X","Y"),each = 40),
var.test2 = rep(c("A","B","C","D"), times = 20),
val.test = rnorm(80,4,5),
x = c(1:80)
)
my.formula <- c(y~x,y~x,y~x,y~x,y~x + I(x^2), y~x + I(x^2),y~x + I(x^2), y ~x)
ggplot(df, aes(x = x, y = val.test)) +
geom_point() +
mapply(function(x, z) {
data <- subset(df, var.test == z)
list(
geom_smooth(
method = "glm", data = data, formula = x,
method.args = list(family = "poisson"), color = "black"
),
stat_poly_eq(formula = x, aes(label = ..eq.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = -0.1),
stat_poly_eq(formula = x, aes(label = ..rr.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = 1.1)
)
}, my.formula, c("A", "B", "C","D","X","Y")) +
facet_grid(var.test2~var.test)
据我所知有两个问题。首先,您的回归是泊松回归,但您的数据不是计数数据 - 它包含负值,因此有时会完全失败,即使没有负值也是不合适的。我很感激这只是示例数据,因此它可能与您的实际用例无关。
其次,更重要的是对于您的实际用例,您的映射函数不太正确。您需要通过 both 分面变量进行子集化,以便您拥有 8 组独特的层 - 每个层用于 var.test
和 var.test2
.[=14= 的每个交互]
ggplot(df, aes(x = x, y = val.test)) +
geom_point() +
mapply(function(x, z, v) {
data <- subset(df, var.test2 == z & var.test == v)
list(
geom_smooth(
method = "glm", data = data, formula = x,
method.args = list(family = "gaussian"), color = "black"
),
stat_poly_eq(formula = x, aes(label = ..eq.label..),
parse = TRUE, size = 2.5, col = "black",
data = data, vjust = -0.1),
stat_poly_eq(formula = x, aes(label = ..rr.label..),
parse = TRUE, size = 2.5, col = "black",
data = data, vjust = 1.1)
)
},
x = my.formula,
z = rep(c("A", "B", "C","D"), 2),
v = rep(c("X", "Y"), each = 4)) +
facet_grid(var.test2~var.test)
对于 X 列,所有图都需要拟合线性回归,而对于 Y 列,则需要线性和二次的混合。但是,在图中,所有拟合都是二次的。在某些情况下也没有拟合线。对调整代码有什么建议吗?
set.seed(14)
df <- data.frame(
var.test = rep(c("X","Y"),each = 40),
var.test2 = rep(c("A","B","C","D"), times = 20),
val.test = rnorm(80,4,5),
x = c(1:80)
)
my.formula <- c(y~x,y~x,y~x,y~x,y~x + I(x^2), y~x + I(x^2),y~x + I(x^2), y ~x)
ggplot(df, aes(x = x, y = val.test)) +
geom_point() +
mapply(function(x, z) {
data <- subset(df, var.test == z)
list(
geom_smooth(
method = "glm", data = data, formula = x,
method.args = list(family = "poisson"), color = "black"
),
stat_poly_eq(formula = x, aes(label = ..eq.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = -0.1),
stat_poly_eq(formula = x, aes(label = ..rr.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = 1.1)
)
}, my.formula, c("A", "B", "C","D","X","Y")) +
facet_grid(var.test2~var.test)
据我所知有两个问题。首先,您的回归是泊松回归,但您的数据不是计数数据 - 它包含负值,因此有时会完全失败,即使没有负值也是不合适的。我很感激这只是示例数据,因此它可能与您的实际用例无关。
其次,更重要的是对于您的实际用例,您的映射函数不太正确。您需要通过 both 分面变量进行子集化,以便您拥有 8 组独特的层 - 每个层用于 var.test
和 var.test2
.[=14= 的每个交互]
ggplot(df, aes(x = x, y = val.test)) +
geom_point() +
mapply(function(x, z, v) {
data <- subset(df, var.test2 == z & var.test == v)
list(
geom_smooth(
method = "glm", data = data, formula = x,
method.args = list(family = "gaussian"), color = "black"
),
stat_poly_eq(formula = x, aes(label = ..eq.label..),
parse = TRUE, size = 2.5, col = "black",
data = data, vjust = -0.1),
stat_poly_eq(formula = x, aes(label = ..rr.label..),
parse = TRUE, size = 2.5, col = "black",
data = data, vjust = 1.1)
)
},
x = my.formula,
z = rep(c("A", "B", "C","D"), 2),
v = rep(c("X", "Y"), each = 4)) +
facet_grid(var.test2~var.test)