简体   繁体   English

在 r 中使用 ggplot2 按组绘制置信区间

[英]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.谁能解释这里发生了什么。

simulated data模拟数据

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))

These codes generate what I want, but there is an improper argument name (aaaaa) in 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

我想要的情节(但参数名称错误)

After removing this argument, there is an error message and no plot is generated去掉这个参数后,有错误提示,没有生成plot

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()以查看错误发生的位置。

After correcting this argument name to a reasonable one, the figure went wrong将此参数名称更正为合理的名称后,数字出错了

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审美应该是educeduc_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM