错误:没有名为StatHline的统计信息

时间:2016-01-13 05:43:24

标签: r error-handling ggplot2

我的数据框如下:

variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
sumVarVal=data.frame(variable=variable,value=value,Count=Count,mean=mean)

我可以做一个很好的情节(正方形的大小对应于具有特定x值和y值的观察计数),如下所示:

library(ggplot2)
ggplot(sumVarVal, aes(variable, value)) + 
  geom_point(aes(size = Count), pch=15) + 
  guides(fill=guide_legend(title="New")) + 
  theme(legend.background = element_rect(fill="gray90", 
        size=.5, 
        colour = "black"), 
        legend.text=element_text(size=rel(1.3)), 
        legend.title=element_text(size=rel(1.3), face="plain"), 
        legend.position="bottom", 
        axis.text = element_text(size=rel(1.3)), 
        axis.title = element_text(size = rel(1.3))) + 
  labs(x="Learning Outcome", y = "Percentage Grade")

但是,我曾经有一段额外的代码(在语法结尾处),这允许我在四个主题中的每一个上叠加一个水平条,表示平均百分比等级。这些值存储在df $ mean中。这是我正在使用的代码:

ggplot(sumVarVal, aes(variable, value)) +
  geom_point(aes(size = Count), pch=15) + 
  guides(fill=guide_legend(title="New")) + 
  theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"), 
        legend.text=element_text(size=rel(1.3)), 
        legend.title=element_text(size=rel(1.3), face="plain"), 
        legend.position="bottom", 
        axis.text = element_text(size=rel(1.3)),
        axis.title = element_text(size = rel(1.3))) + 
  labs(x="Learning Outcome", y = "Percentage Grade") + 
  geom_errorbar(stat = "hline", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))

使用版本1.0.1,这会给出:

enter image description here

使用版本2.0.0,现在会导致错误:

  

错误:没有名为StatHline的统计信息。

我知道这可能与ggplot2中最近的升级有关。我已经看到了最近关于它的其他评论(geom_errorbar - "No stat called StatHline")。但是,由于我的代码围绕stat="hline"的使用,当我尝试其中一些建议时,我无法让我的代码工作。也许我对原始代码有些不了解,这使我无法更新此问题?

编辑:我已经考虑了一些建议,目前正在使用此代码:

ggplot(sumVarVal, aes(variable, value)) +
  geom_point(aes(size = Count), pch=15) + 
  guides(fill=guide_legend(title="New")) + 
  theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"), 
        legend.text=element_text(size=rel(1.3)), 
        legend.title=element_text(size=rel(1.3), face="plain"), 
        legend.position="bottom", 
        axis.text = element_text(size=rel(1.3)),
        axis.title = element_text(size = rel(1.3))) + 
  labs(x="Learning Outcome", y = "Percentage Grade") + 
  geom_errorbar(stat = "summary", fun.y = "mean", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))

这给了我一个如下所示的输出:

My current output

似乎有些平均蓝线没有排列到它们的值,正如最初在平均向量中给出的那样。例如,对于变量" D",它的平均值应为93.3,但蓝色水平线似乎显示为90.0的值。在我的真实代码(不是这个MWE)中,效果更加显着。可能导致这种差异的任何想法?

1 个答案:

答案 0 :(得分:0)

stat_hline got removed in ggplot2 2.0.0,但从不担心;反正它并不是真的有必要。如果您完全删除stat参数,则默认为identity,这很好。 (summary如果您愿意也可以使用。)但是,您需要更改aes映射,将yintercept更改为y以考虑新stat }}

一起,

ggplot(sumVarVal, aes(variable, value)) +
  geom_point(aes(size = Count), pch=15) + 
  guides(fill=guide_legend(title="New")) + 
  theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"), 
        legend.text=element_text(size=rel(1.3)), 
        legend.title=element_text(size=rel(1.3), face="plain"), 
        legend.position="bottom", 
        axis.text = element_text(size=rel(1.3)),
        axis.title = element_text(size = rel(1.3))) + 
  labs(x="Learning Outcome", y = "Percentage Grade") + 
  geom_errorbar(width=0.6, colour = "blue", size = 1, aes(ymax=..y.., ymin=..y.., y = mean))

产生

plot with mean bars in right place