在 xy.coords(x, y) 中出现错误:将平均值绘制到箱线图上时,'x' 和 'y' 长度

问题描述 投票:0回答:1

我正在尝试将一些平均值绘制到包含三组的箱线图上。我的代码生成了正确的箱线图,但无法绘制平均值。我已经对其他数据集/csv 文件尝试了相同的代码,并且效果很好,因为我既有箱线图又有平均值。

这是箱线图的代码:

boxplot(siteD$Average_Ground ~ siteD$Sampling_Period, notch=F)

我检查了这两个变量,它们的长度相等。

这是绘制平均值的代码:

meanz <- tapply(siteD$Average_Ground, siteD$Sampling_Period, mean, na.rm=T)
points(seq(1, nlevels(siteD$Sampling_Period)), meanz, 
       col = "red3", pch = 3, lwd = 2, cex = 1.2)

尝试绘图时,出现以下错误:xy.coords(x, y) 中的错误:'x' 和 'y' 长度不同

正如我所说,这段代码适用于具有类似特征的其他数据集,所以我不确定问题是什么。

r boxplot
1个回答
0
投票

我想这就是你正在寻找的

meanz <- tapply(iris$Sepal.Length, iris$Species, mean, na.rm=TRUE)
points(seq(1, nlevels(iris$Species)), meanz, 
       col = "red3", pch = 3, lwd = 2, cex = 1.2)

boxplot(Sepal.Length ~ Species, data = iris, notch=F)

boxplots with means

我不确定您为什么会收到该错误,但您可能缺少值(也许尝试

na.rm = FALSE
)。如果没有看到您的数据,就很难知道。

© www.soinside.com 2019 - 2024. All rights reserved.