我有以下数据框和绘图代码:
d <- structure(list(a = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("-20", "20-", "40-",
"50-"), class = "factor"), tci = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), Score = c(1, 2, 3, 4, 5,
6, 7, 8, 40.7, 51.9, 14.8, 3.7, 15, 75, 35, 20)), .Names = c("Foo",
"bar", "Score"), row.names = c(NA, -16L), class = "data.frame")
library(ggplot2)
p <-ggplot(d,aes(x=Foo,y=Score,fill=bar))+geom_bar(position="dodge",stat="identity")
我想要做的是在列上添加p值括号,如下所示:
但为什么这段代码失败了:
> p + geom_path(x=c(1,1,2,2),y=c(42,45,45,42))
Error: Incompatible lengths for set aesthetics: x, y
答案 0 :(得分:5)
一种解决方案是将x
和y
放入数据框
p + geom_path(data=data.frame(x=c(0.75,0.75,1.25,1.25),y=c(42,45,45,42)),
aes(x,y),inherit.aes=FALSE)
另一种解决方案是使用annotate()
代替geom_path()
。
p + annotate(x=c(0.75,0.75,1.25,1.25),y=c(42,45,45,42),"path")