R - ggplot geom_smooth facet_grid CI 未显示
R - ggplot geom_smooth facet_grid CI not showing
我很难理解为什么我的数据没有显示置信区间。当我在另一个数据集上重现我的代码时,代码似乎工作正常。例如,在 mtcars
密码是
mtols = mtcars %>% group_by(am) %>% do(lm0 = lm(disp ~ mpg*gear + vs, data=.)) %>%
augment(., lm0) %>%
mutate(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit)
生成情节
mtols %>% ggplot(aes(mpg, .fitted) ) +
geom_smooth(data = mtols, aes(mpg, .fitted, group = gear, colour = gear, fill= gear), method="lm") +
theme_minimal() + facet_grid(~am)
我得到了置信区间。
但这不适用于我的数据。有人可以帮我弄清楚这里出了什么问题吗?我会很感激。
我用
计算 OLS
dt = new %>% group_by(day) %>% do(lm0 = lm(y ~ year*class, data=.)) %>% augment(., lm0) %>%
mutate(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit)
dt$year = as.numeric(as.character(dt$year))
情节,(这是一个案例很少的例子,但结果与整个数据集相同)
dt %>% ggplot(aes(year, .fitted) ) +
geom_smooth(data = dt, aes(year, .fitted, group = class, colour = class, fill= class), method="lm") +
theme_bw() + facet_grid(~day)
CI
未显示。
知道我做错了什么吗?
奇怪的是,当我在这里不使用 facet_grid
时,CI
工作得很好
dt %>% ggplot(aes(year, .fitted) ) +
geom_smooth(data = dt, aes(year, .fitted, group = class, colour = class, fill= class), method="lm") +
theme_bw()
我的数据样本
library(broom)
library(dplyr)
library(ggplot2)
new = structure(list(id = structure(c(844084L, 114510L, 14070410L,
942483L, 13190105L, 421369L, 301384L, 251789L, 11011210L, 11280408L,
278575L, 310410L, 16260105L, 11110815L, 18260101L, 14260501L,
10580L, 15090210L, 19140410L, 13230615L, 246511L, 20040812L,
14260114L, 287623L, 16090620L, 20131007L, 835743L, 453390L, 395808L,
363617L), label = "Household identifier", class = c("labelled",
"integer")), year = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L), .Label = c("2000", "2015"), class = "factor"),
day = c("Weekend", "Weekend", "Weekend", "Weekdays", "Weekdays",
"Weekend", "Weekdays", "Weekend", "Weekend", "Weekdays",
"Weekend", "Weekdays", "Weekdays", "Weekend", "Weekend",
"Weekdays", "Weekdays", "Weekend", "Weekdays", "Weekdays",
"Weekdays", "Weekend", "Weekend", "Weekend", "Weekend", "Weekend",
"Weekend", "Weekdays", "Weekdays", "Weekdays"), class = structure(c(1L,
1L, 2L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 2L, 1L, 4L, 1L, 3L,
2L, 3L, 2L, 4L, 2L, 1L, 3L, 2L, 1L, 4L, 3L, 2L, 4L, 1L), .Label = c("Higher Managerial",
"Lower Managerial", "Intermediate", "Manual and Routine"), class = "factor"),
y = c(270, 730, 180, 0, 0, 290, 90, 650, 510, 0, 10, 200,
200, 180, 0, 0, 140, 260, 110, 740, 260, 0, 390, 610, 0,
0, 500, 0, 10, 170)), class = "data.frame", row.names = c(NA,
-30L), .Names = c("id", "year", "day", "class", "y"))
正在绘制置信区间。我们看不到它们,因为每个 day
.
只有两个唯一点
dt2 <- dt %>% filter(class == "Higher Managerial")
plot(.fitted ~ year, data=subset(dt2, day=="Weekend"))
我们看到间隔没有小平面的原因是因为当有四个点时间隔更宽。
当我们不按面突破时,有足够的点在置信度上有一定的范围。但是两点的置信区间是没有范围的。
confint(lm(.fitted ~ year, data=subset(dt2, day=="Weekdays")))
# 2.5 % 97.5 %
# (Intercept) 9503.333333 9503.333333
# year -4.666667 -4.666667
编辑
这里是一个版本,我们使用了最初计算的ymin
和ymax
,并用geom_ribbon
.
绘制了它
dt %>% ggplot(aes(year, .fitted,group = class, colour = class, fill= class)) +
geom_line() +
geom_ribbon(aes(ymin=ymin, ymax=ymax), alpha=0.2) +
theme_bw() + facet_grid(~day)
我很难理解为什么我的数据没有显示置信区间。当我在另一个数据集上重现我的代码时,代码似乎工作正常。例如,在 mtcars
密码是
mtols = mtcars %>% group_by(am) %>% do(lm0 = lm(disp ~ mpg*gear + vs, data=.)) %>%
augment(., lm0) %>%
mutate(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit)
生成情节
mtols %>% ggplot(aes(mpg, .fitted) ) +
geom_smooth(data = mtols, aes(mpg, .fitted, group = gear, colour = gear, fill= gear), method="lm") +
theme_minimal() + facet_grid(~am)
我得到了置信区间。
但这不适用于我的数据。有人可以帮我弄清楚这里出了什么问题吗?我会很感激。
我用
计算OLS
dt = new %>% group_by(day) %>% do(lm0 = lm(y ~ year*class, data=.)) %>% augment(., lm0) %>%
mutate(ymin=.fitted-1.96*.se.fit, ymax=.fitted+1.96*.se.fit)
dt$year = as.numeric(as.character(dt$year))
情节,(这是一个案例很少的例子,但结果与整个数据集相同)
dt %>% ggplot(aes(year, .fitted) ) +
geom_smooth(data = dt, aes(year, .fitted, group = class, colour = class, fill= class), method="lm") +
theme_bw() + facet_grid(~day)
CI
未显示。
知道我做错了什么吗?
奇怪的是,当我在这里不使用 facet_grid
时,CI
工作得很好
dt %>% ggplot(aes(year, .fitted) ) +
geom_smooth(data = dt, aes(year, .fitted, group = class, colour = class, fill= class), method="lm") +
theme_bw()
我的数据样本
library(broom)
library(dplyr)
library(ggplot2)
new = structure(list(id = structure(c(844084L, 114510L, 14070410L,
942483L, 13190105L, 421369L, 301384L, 251789L, 11011210L, 11280408L,
278575L, 310410L, 16260105L, 11110815L, 18260101L, 14260501L,
10580L, 15090210L, 19140410L, 13230615L, 246511L, 20040812L,
14260114L, 287623L, 16090620L, 20131007L, 835743L, 453390L, 395808L,
363617L), label = "Household identifier", class = c("labelled",
"integer")), year = structure(c(1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L), .Label = c("2000", "2015"), class = "factor"),
day = c("Weekend", "Weekend", "Weekend", "Weekdays", "Weekdays",
"Weekend", "Weekdays", "Weekend", "Weekend", "Weekdays",
"Weekend", "Weekdays", "Weekdays", "Weekend", "Weekend",
"Weekdays", "Weekdays", "Weekend", "Weekdays", "Weekdays",
"Weekdays", "Weekend", "Weekend", "Weekend", "Weekend", "Weekend",
"Weekend", "Weekdays", "Weekdays", "Weekdays"), class = structure(c(1L,
1L, 2L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 2L, 1L, 4L, 1L, 3L,
2L, 3L, 2L, 4L, 2L, 1L, 3L, 2L, 1L, 4L, 3L, 2L, 4L, 1L), .Label = c("Higher Managerial",
"Lower Managerial", "Intermediate", "Manual and Routine"), class = "factor"),
y = c(270, 730, 180, 0, 0, 290, 90, 650, 510, 0, 10, 200,
200, 180, 0, 0, 140, 260, 110, 740, 260, 0, 390, 610, 0,
0, 500, 0, 10, 170)), class = "data.frame", row.names = c(NA,
-30L), .Names = c("id", "year", "day", "class", "y"))
正在绘制置信区间。我们看不到它们,因为每个 day
.
dt2 <- dt %>% filter(class == "Higher Managerial")
plot(.fitted ~ year, data=subset(dt2, day=="Weekend"))
我们看到间隔没有小平面的原因是因为当有四个点时间隔更宽。
当我们不按面突破时,有足够的点在置信度上有一定的范围。但是两点的置信区间是没有范围的。
confint(lm(.fitted ~ year, data=subset(dt2, day=="Weekdays")))
# 2.5 % 97.5 %
# (Intercept) 9503.333333 9503.333333
# year -4.666667 -4.666667
编辑
这里是一个版本,我们使用了最初计算的ymin
和ymax
,并用geom_ribbon
.
dt %>% ggplot(aes(year, .fitted,group = class, colour = class, fill= class)) +
geom_line() +
geom_ribbon(aes(ymin=ymin, ymax=ymax), alpha=0.2) +
theme_bw() + facet_grid(~day)