我打算创建一个箱线图并突出显示成对比较的显着性水平。这已在 previous post. 中处理
当我对我的数据集做同样的事情时,我收到以下错误:
"Incompatible lengths for set aesthetics: x, y"
这是一个示例数据集来说明问题 -
data1<-data.frame(island = c("A", "B", "B", "A", "A"), count = c(2, 5, 12, 2, 3))
g1<-ggplot(data1) + geom_boxplot(aes(x = factor(island), y = count))
g1 + geom_path(x = c(1, 1, 2, 2), y = c(25, 26, 26, 25))
运行代码的第三行时出现错误,而箱线图结果正常。我怀疑我错过了一些非常微不足道的东西,但我无法捕获它。我将不胜感激任何帮助。
最佳答案
因为你没有明确的 data
参数在 geom_path
,数据来自 data
参数在 ggplot
被“继承”到 geom_path
.当发现“data1”中的 x 和 y 变量的长度与 geom_path
中的 x 和 y 向量的长度不同时,机器就会窒息。称呼。尝试为 geom_path
创建一个单独的数据框并使用 data
争论:
data2 <- data.frame(x = c(1, 1, 2, 2), y = c(25, 26, 26, 25))
ggplot(data = data1, aes(x = factor(island), y = count)) +
geom_boxplot() +
geom_path(data = data2, aes(x = x, y = y))
![enter image description here](https://i.sstatic.net/H2xsL.png)
关于r - 使用 ggplot 函数将 geom_path 添加到 boxplot 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658388/