使用ggplot函数将geom_path添加到boxplot时出错 [英] error with adding geom_path to boxplot using ggplot function
问题描述
我打算创建一个箱形图,并突出显示成对比较的显着性水平. 以前的帖子中对此进行了处理.
I intend to create a boxplot and highlight the significance level of pairwise comparisons. This has been dealt in a previous post.
当我对数据集执行相同操作时,出现以下错误:
When I do the same for my data-set, I get the following error:
"Incompatible lengths for set aesthetics: x, y"
这是一个说明问题的示例数据集-
Here's an example data-set to illustrate the problem -
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))
在运行代码的第三行时出现错误,而箱线图结果还不错.我怀疑我错过了一些非常琐碎的东西,但是我无法抓住它.非常感谢您的帮助.
I get the error while running the third line of the code, while the boxplot turns out alright. I suspect I'm missing out on something very trivial, but I'm unable to catch it. I'd greatly appreciate any help.
推荐答案
由于geom_path
中没有显式的data
自变量,因此将ggplot
中data
自变量中的数据继承"为geom_path
.然后,当发现"data1"中x和y变量的长度与geom_path
调用中x和y向量的长度不同时,该机制就会感到窒息.尝试为geom_path
创建一个单独的数据框,并使用data
参数:
Because you don't have an explicit data
argument in geom_path
, data from the data
argument in ggplot
is 'inherited' to geom_path
. The machinery then chokes when it finds out that the length of the x and y variables in 'data1' differs from the length of the x and y vectors in the geom_path
call. Try to create a separate data frame for geom_path
and use the data
argument:
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))
这篇关于使用ggplot函数将geom_path添加到boxplot时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!