我将此添加为答案,因为它太长而无法成为评论,但它是作为已接受答案的补充。
在类似的情况下,我尝试使用geom_path
彩色条形图,但出现此错误:
Error in eval(expr, envir, enclos) : object 'Species' not found
然后事实证明该fill
选项应该“关闭”,否则它会跟随前面ggplot
调用中的那个,它要求Species
列并导致这样的错误。
## ## 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
