我有一个数据框,如下所示:

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版本,可以得到:

r - 错误: no stat called StatHline-LMLPHP

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



我知道这可能与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))

这给我的输出看起来像这样:

r - 错误: no stat called StatHline-LMLPHP

似乎某些均值蓝线未与其均值 vector 中最初给出的值对齐。例如,对于变量“D”,其平均值应为93.3,但蓝色水平线似乎显示为90.0。在我的真实代码(不是此MWE)中,效果甚至更加明显。有什么想法可能导致这种差异吗?

最佳答案

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

产生

r - 错误: no stat called StatHline-LMLPHP

关于r - 错误: no stat called StatHline,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34759193/

10-12 23:31