【问题标题】:Confidence intervals in the wrong place on a lattice barchart格子条形图上错误位置的置信区间
【发布时间】:2016-02-24 17:51:31
【问题描述】:

我想创建一个带有置信区间的以下数据的条形图:

IVs <- expand.grid(fault=c("L", "H"), moral=c("L", "N","H"),     verdict=c("Not Guilty", "Guilty"))
freq<-c( 8, 24, 12, 41, 4, 11, 32, 17, 79, 65, 42, 23)
(  Question2 <- data.frame(IVs, freq=freq)  )

我使用逻辑回归模型计算每种情况下有罪判决的概率,并使用 lsmeans 计算这些概率的 CI。

library(lsmeans)

m3.lr<-glm(verdict ~ fault * moral, data=Question2, family=binomial, weights=freq)
(  m3.lr.lsm <- lsmeans(m3.lr, ~fault*moral, type="response")  )

然后我想用它来创建这个数据的条形图。我已使用此代码的变体在此数据集中创建其他条形图,并且效果很好。但是,现在我想使用代码创建所有六个条件的条形图,CI 出现在错误的位置。 CIs 线出现在每个道德品格组的中间,即使它们各自属于单独的条形。我似乎无法弄清楚如何将 CI 行移动到各自的栏。任何帮助将不胜感激!

我用来创建图表的代码是:

library(latticeExtra)

barchart(prob ~ moral, groups=fault, data=summary(m3.lr.lsm), 
     ylim=c(-0.05,1.05), xlab="Moral Character", ylab="Probability Guilty") + 
 layer(panel.key(c("green=Low Victim Fault", "pink=High Victim Fault"), 
              corner=c(0, 1), lines=FALSE, points=FALSE)) +
 layer(with(summary(m3.lr.lsm), panel.arrows(moral, asymp.LCL, moral, asymp.UCL, ends="both", angle=90, length=0.1)))

这是我当前的图表,其中 CI 线在错误的位置。

【问题讨论】:

  • 这是 ggplot,不是 lattice,所以我是在评论而不是回答:library(ggplot2); ggplot(summary(m3.lr.lsm), aes(x = moral, y=prob, fill=fault)) +geom_bar(stat="identity", position = position_dodge(1)) + geom_errorbar(aes(ymin = asymp.LCL, ymax = asymp.UCL), position = position_dodge(1), width = 0.2)
  • 谢谢!我很乐意使用 ggplot 而不是 lattice,所以这很完美。
  • 没问题,我会添加它作为答案

标签: r bar-chart lattice logistic-regression confidence-interval


【解决方案1】:

问题是 lattice 无法确定您的条形图中是否有子组。在格子中很难修复 - 这是一个 gpplot 修复,使用 position_dodge:

library(ggplot2)
ggplot(summary(m3.lr.lsm), aes(x = moral, y = prob, fill = fault)) + 
   geom_bar(stat = "identity", position = position_dodge(0.9)) +
   geom_errorbar(aes(ymin = asymp.LCL, ymax = asymp.UCL), position = position_dodge(0.9), width = 0.2)

【讨论】:

    猜你喜欢
    • 2021-04-08
    • 2020-02-27
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    相关资源
    最近更新 更多