Simon W Simon W - 2 months ago 16
R Question

Error in eval(expr, envir, enclos) : object 'Tribe' not found

I am following some online examples for getting to grips with ggplot2 in R. As part of this I am trying to add in the automatic coloring of series using pre-defined color schemes.

The following code will work:

ggplot(subset(homes, STATE %in% c("MA","TX"))) + geom_point(aes_string(x="Date", y="Home.Value"))


However, when I add the color option:

ggplot(subset(homes, STATE %in% c("MA","TX"))) + geom_point(aes_string(x="Date", y="Home.Value"), color=State)


I get the error: "Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint, :
object 'State' not found"


I have also tried 'Tribe' and 'cp1' with no luck and the same error message. Is there anything I need to do to bring the auto color options into the software so the objects are recognised?

Thanks!

Answer

You want the colour of your points to vary by the variable state, so it should be inside the aes:

 geom_point(aes(colour = State))

Anything outside of the aes is constant, so, colour=red or size=2.

Comments