ggplot 中的 Facet-wrap 函数
Facet-wrap in ggplot with function
@Allan Cameron 帮助我编写了一个代码,当我绘制两个变量时,它会自动将 R2 和 p 值(来自 LM)放在图表的左上角。
但是我不能让它与 facet_wrap 一起工作。例如,在 mtcars 数据集中,如果我想针对 disp 绘制 wt,我可以这样做,但如果我想使用 facet_wrap 为每个气缸组(例如 4、6、8)绘制图表,那么我收到以下错误消息
At least one layer must contain all faceting variables: `cyl`.
* Plot is missing `cyl`
* Layer 1 is missing `cyl`
* Layer 2 is missing `cyl`
* Layer 3 is missing `cyl`
* Layer 4 is missing `cyl`
这是代码
ggplotRegression <- function (fit, title) {
require(ggplot2)
lab <- grid::textGrob(label = paste0(
as.character(as.expression(fit$call$formula)), "\n",
"Adj R\u00b2 = ",
signif(summary(fit)$adj.r.squared, 1),
", p = ", signif(summary(fit)$coef[2,4], 1)),
x = unit(0.05, "npc"),
y = unit(0.9, "npc"), just = "left",
gp = grid::gpar(size = 14, fontface = "bold"))
ggplot(fit$model, aes_string(x = names(fit$model)[2],
y = names(fit$model)[1])) +
ggtitle(title) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
annotation_custom(lab)
}
ggplotRegression(lm(disp ~ wt, data = mtcars), "My Title") +
geom_point(size = 3.74, colour = "#0c4c8a") +
theme_bw()+
facet_wrap(vars(cyl), scales = "free")
另外,我不太明白代码的作用?
你运行进入错误是因为函数依赖fit$model
,如果你将lm(disp ~ wt, data = mtcars)
输入数据,你可以看到ggplot中不再有cyl对象:
fit = lm(disp ~ wt, data = mtcars)
head(fit$model)
disp wt
Mazda RX4 160 2.620
Mazda RX4 Wag 160 2.875
Datsun 710 108 2.320
Hornet 4 Drive 258 3.215
Hornet Sportabout 360 3.440
Valiant 225 3.460
@Allan 为您提供的基本上是编辑 1 个情节并插入文本中。如果你想在不同的数据子集上拟合三个线性模型,那就是:
library(ggpmisc)
library(ggplot2)
formula = y~x
ggplot(mtcars, aes(wt, disp)) +
geom_point() +
geom_smooth(method = "lm",formula=formula) +
facet_wrap(~cyl, scales = "free")+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label), stat(p.value.label),sep = "*\", \"*")),
formula = formula, parse = TRUE,size=3)
@Allan Cameron 帮助我编写了一个代码,当我绘制两个变量时,它会自动将 R2 和 p 值(来自 LM)放在图表的左上角。
但是我不能让它与 facet_wrap 一起工作。例如,在 mtcars 数据集中,如果我想针对 disp 绘制 wt,我可以这样做,但如果我想使用 facet_wrap 为每个气缸组(例如 4、6、8)绘制图表,那么我收到以下错误消息
At least one layer must contain all faceting variables: `cyl`.
* Plot is missing `cyl`
* Layer 1 is missing `cyl`
* Layer 2 is missing `cyl`
* Layer 3 is missing `cyl`
* Layer 4 is missing `cyl`
这是代码
ggplotRegression <- function (fit, title) {
require(ggplot2)
lab <- grid::textGrob(label = paste0(
as.character(as.expression(fit$call$formula)), "\n",
"Adj R\u00b2 = ",
signif(summary(fit)$adj.r.squared, 1),
", p = ", signif(summary(fit)$coef[2,4], 1)),
x = unit(0.05, "npc"),
y = unit(0.9, "npc"), just = "left",
gp = grid::gpar(size = 14, fontface = "bold"))
ggplot(fit$model, aes_string(x = names(fit$model)[2],
y = names(fit$model)[1])) +
ggtitle(title) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
annotation_custom(lab)
}
ggplotRegression(lm(disp ~ wt, data = mtcars), "My Title") +
geom_point(size = 3.74, colour = "#0c4c8a") +
theme_bw()+
facet_wrap(vars(cyl), scales = "free")
另外,我不太明白代码的作用?
你运行进入错误是因为函数依赖fit$model
,如果你将lm(disp ~ wt, data = mtcars)
输入数据,你可以看到ggplot中不再有cyl对象:
fit = lm(disp ~ wt, data = mtcars)
head(fit$model)
disp wt
Mazda RX4 160 2.620
Mazda RX4 Wag 160 2.875
Datsun 710 108 2.320
Hornet 4 Drive 258 3.215
Hornet Sportabout 360 3.440
Valiant 225 3.460
@Allan 为您提供的基本上是编辑 1 个情节并插入文本中。如果你想在不同的数据子集上拟合三个线性模型,那就是:
library(ggpmisc)
library(ggplot2)
formula = y~x
ggplot(mtcars, aes(wt, disp)) +
geom_point() +
geom_smooth(method = "lm",formula=formula) +
facet_wrap(~cyl, scales = "free")+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label), stat(p.value.label),sep = "*\", \"*")),
formula = formula, parse = TRUE,size=3)