3

I am plotting a geom_path object and a geom_text object in the same ggplot but am running into the following problem:

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

But get the error Error in eval(expr, envir, enclos) : object 'site' not found

Any ideas?

2
  • 2
    Every aesthetic in the main ggplot call is expected in every subsequent geom_. I suppose I should add that the solution is either to move label = site or unmap it in geom_path by setting it to NULL. Commented Sep 27, 2012 at 20:39
  • Thanks. I added the label call and that worked. Perhaps move you comment to an answer so I can accept it? Commented Sep 27, 2012 at 20:51

2 Answers 2

7

Every aesthetic in the main ggplot call is expected in every subsequent geom_. The solution is either to move label = site or unmap it in geom_path by setting it to NULL there.

Sign up to request clarification or add additional context in comments.

Comments

4
ggplot(df1, aes(x, y)) + 
geom_text(aes(label = site, colour = factor(Group)), size = 4) +
coord_fixed() + geom_path(df2, aes(x, y)) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.