[英]Plotting confidence interval by group with ggplot2 in r
I would like to plot confidence intervals with shadow bands by groups in R.我想 plot 置信区间和 R 中的组阴影带。 The ideal case is that the color of all bands stays the same (grey).理想的情况是所有波段的颜色保持不变(灰色)。 I made some typos in ggplot2 and it somehow generates the plot I was expecting.我在 ggplot2 中做了一些错别字,它以某种方式生成了我期待的 plot。 While after correcting the typos, I could never get the plot I want.在更正错别字之后,我永远无法得到我想要的 plot。 Could anyone explain what is going on here.谁能解释这里发生了什么。
set.seed(42)
dat <- data.frame(educ = rep(c("level 1","level 2","level 3"),each = 10),
age = 65:69,value = c(seq(1,5,by=1),seq(1,5,by=1)-1,
seq(5,9,by=1),seq(5,9,by=1)-1,
seq(10,14,by=1),seq(10,14,by=1)-1),
sd = abs(rnorm(30)/10),
educ_child = rep(rep(c("HS","HS and above"),each= 5),3))
geom_ribbon
and a warning message.这些代码生成我想要的,但是geom_ribbon
中有一个不正确的参数名称 (aaaaa) 和一条警告消息。ggplot(dat,
aes(x = age,y=value,color = educ,linetype=educ_child))+
geom_line()+
geom_ribbon(aes(ymin = value-1.96*sd,
ymax = value+1.96*sd,aaaaa=educ_child,linetype=NA),alpha = .2)+
theme_light()
The warning message is:警告信息是:
Warning message: Ignoring unknown aesthetics: aaaaa警告信息:忽略未知的美学:aaaaa
ggplot(dat,
aes(x = age,y=value,color = educ,linetype=educ_child))+
geom_line()+
geom_ribbon(aes(ymin = value-1.96*sd,
ymax = value+1.96*sd,group=educ_child,linetype=NA),alpha = .2)+
theme_light()
the error message is:错误信息是:
Error in f()
: ! f()
中的错误:! Aesthetics can not vary with a ribbon Run rlang::last_error()
to see where the error occurred.美学不能随功能区而变化运行rlang::last_error()
以查看错误发生的位置。
ggplot(dat,
aes(x = age,y=value,color = educ,linetype=educ_child))+
geom_line()+
geom_ribbon(aes(ymin = value-1.96*sd,
ymax = value+1.96*sd,linetype=NA),alpha = .2)+
theme_light()
The group
aesthetic should be the interaction between educ
and educ_child
, since there are separate ribbons for each subset within both of these variables: group
审美应该是educ
和educ_child
之间的交互,因为这两个变量中的每个子集都有单独的功能区:
ggplot(dat,
aes(x = age, y = value, color = educ, linetype = educ_child)) +
geom_line() +
geom_ribbon(aes(ymin = value - 1.96 * sd,
ymax = value + 1.96 * sd,
group = interaction(educ, educ_child), linetype = NA),
alpha = 0.2)+
theme_light()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.