error with adding geom_path to boxplot using ggplo

2019-06-26 08:21发布

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.

标签: r ggplot2
2条回答
我只想做你的唯一
2楼-- · 2019-06-26 08:58

I'm adding this as an answer, because it is too long to be a comment, but it is meant as a supplement to the already accepted answer.

In a similar situation I tried employing geom_path to a colored barplot, but got this error:

Error in eval(expr, envir, enclos) : object 'Species' not found

Then it turns out that the fill option should be turned "off", otherwise it follows the one in the preceding ggplot call, which asks for the Species column and leads such error.

## ## Load the libraries
require(data.table)
require(ggplot2)

## ## Make toy data
data1 <- data.table(iris)[,list(value=Petal.Length,Species)]

## ## Draw the bars
p <- ggplot(data=data1,aes(x=Species,y=value,fill=Species)) +
  geom_boxplot() +
  scale_x_discrete(breaks=NULL)

## ## Add lines and an annotation
y1 <- data1[Species=="setosa",max(value)]*1.02
y2 <- y3 <- data1[,max(value)]*1.05
y4 <- data1[Species=="virginica",max(value)]*1.005
data2 <- data.frame(x.=c(1,1,3,3),y.=c(y1,y2,y3,y4))

p <- p +
  ## geom_path(data=data2,aes(x=x.,y=y.)) + # the line that cause the ERROR
  geom_path(data=data2,aes(x=x.,y=y.,fill=NULL)) + # the corrected line
  annotate("text",x=2,y=y3,label="***")
p

enter image description here

查看更多
女痞
3楼-- · 2019-06-26 09:06

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

enter image description here

查看更多
登录 后发表回答