3

我在同一个 ggplot 中绘制了一个 geom_path 对象和一个 geom_text 对象,但遇到了以下问题:

#load the data frames
df1 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25), grp=c(1, 2, 2), site=c("A", "B", "C"))
df1$grp = factor(df1$grp)
colnames(df1)[3] = "Group"

df2 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25))

#create basic plot with site name coloured by group 
p = ggplot(df1, aes(x=x, y=y, label=site))
p = p + geom_text(aes(colour=factor(Group)), size=4)
p = p + coord_fixed()

#I try adding a path
p = p + geom_path(data=df2, aes(x=x, y=y)) 

但是在 eval(expr, envir, enclos) 中得到错误 Error : object 'site' not found

有任何想法吗?

4

2 回答 2

7

主要 ggplot 调用中的每一种美学都预计在随后的每个 geom_ 中。解决方案是通过将其设置到那里来移动label = site或取消映射。geom_pathNULL

于 2012-09-27T20:52:08.490 回答
3
ggplot(df1, aes(x, y)) + 
geom_text(aes(label = site, colour = factor(Group)), size = 4) +
coord_fixed() + geom_path(df2, aes(x, y)) 
于 2012-09-27T20:52:26.080 回答