R-ggplot geom_光滑面_网格CI未显示
我很难理解为什么我的数据中没有显示置信区间。当我在另一个数据集上复制代码时,代码似乎工作正常。例如,在mtcars上 代码是R-ggplot geom_光滑面_网格CI未显示,r,ggplot2,broom,R,Ggplot2,Broom,我很难理解为什么我的数据中没有显示置信区间。当我在另一个数据集上复制代码时,代码似乎工作正常。例如,在mtcars上 代码是 mtols = mtcars %>% group_by(am) %>% do(lm0 = lm(disp ~ mpg*gear + vs, data=.)) %>% augment(., lm0) %>% mutate(ymin=.fitted-1.96*.se.fit, ymax=.
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)
我得到了置信区间
但是,这不适用于我的数据。有人能帮我找出这里出了什么问题吗?我将非常感激
我使用
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))
绘图(这是一个案例较少的示例,但整个数据集的结果相同)
CI
不显示
知道我做错了什么吗
奇怪的是,当我不在这里使用facet\u网格时,CI
工作得很好
我的数据样本
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"))
正在绘制置信区间。我们无法看到它们,因为每个天只有两个唯一的点
dt2 <- dt %>% filter(class == "Higher Managerial")
plot(.fitted ~ year, data=subset(dt2, day=="Weekend"))
编辑
这里是一个版本,我们使用最初计算的ymin
和ymax
,并使用geom_功能区绘制它
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)
这两张图似乎不一样,这是一个问题,你确定这是组=类而不是组=天?@Mamounbenghzal否组是类,因为我想在天之前显示类*年的交互作用随时间的影响。因此,我希望facet\u网格
将日期的类型分开。感谢该示例产生了一个错误。错误:x
和标签必须相同type@PierreLafortune你指的是哪一个x
?这是你的第一列Classes'labeled',integer'
。但没关系,问题是你需要更多的点才能看到置信区间。是的,你是对的,我只有两天的观察。但理论上,我仍然应该能够围绕条件平均值绘制CI
。所以,当周末和工作日都在一起的时候,它能工作吗?什么是解决方案?你是说它“不起作用”,但它确实起作用了。它正在绘制置信区间,它们不是宽区间。对不起,我不明白。你所说的wide
间隔是什么意思。例如,ggplot(mtcars,aes(am,disp))+geom_point()+geom_smooth(method=“lm”)
仍然绘制置信区间,即使am
只有两个点?您可能要查找的是geom_ribbon
而不是smooth
。因为“平滑”是计算它自己的回归线。你已经演过了。”ribbon'将允许您使用已创建的ymin
和ymax
。我添加了一个带有geom_ribbon
和数据中的ymin和ymax的示例。
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
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)