今天遇到一個異常,代碼如下:
set.seed(12345)
require(ggplot2)
AData <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1))
i <- 2
j <- 3
p <- ggplot(data=AData, aes(AData[, i], AData[, j])) + geom_point() + theme_bw()
p # all right
p <- p + geom_text(aes(data=AData, label=Glabel), size=3, vjust=1.25, colour="black")
p # Error: Aesthetics must either be length one, or the same length as the dataProblems:AData
下面的代碼就ok:
example <- data.frame(r=c(5,4,3),theta=c(0.9,1.1,0.6))
myplot <- ggplot(example, aes(r, theta)) + geom_point(size=3.5) +
geom_text(aes(label=rownames(example)), size=4.4, hjust=0.5, vjust=-1)
根據異常信息,應該是說Glabel與AData的長度不同,但具體什么原因不了解。
stackover上面的解釋是:對於0.9.0版本的ggplot2有這個問題,0.9.1版本中已經解決。(http://stackoverflow.com/questions/10628847/geom-boxplot-with-precomputed-values)
利用installed.packages()命令,看到我的ggplot2的版本是1.0.1,應該沒有問題,奇怪。
stackoverflow上另外一個類似的帖子是:http://stackoverflow.com/questions/20057452/aesthetics-must-either-be-length-one-or-the-same-length-as-the-dataproblems
但是與我遇到的問題不大一樣,暫無解.