1
votes

I am a total newbie at R, just testing the waters, and I am trying to implement a sample code given in this book. This is the code:

qplot(carat, price, data = dsmall, colour = color)

And this is the error I am getting:

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

I copy-pasted the exact code, to make sure I am not making any typos, and the same error still comes up. I have included the ggplot2 package. Is there any package I need to include which I am missing?

2
Have you defined dsmall? What does it look like? Basically the error is telling you that there isn't a column named "color" in your data set. - Gregor Thomas
On page 11 of the booked you linked is the code: dsmall <- diamonds[sample(nrow(diamonds), 100), ]. Did you create that object? If you look at the details ('str(dsmall)'), you should see "color" is a valid column. - oshun
I have seen more than one person overlook that dsmall definition line. It is misleading because diamonds is defined in the ggplot2 library, but then the examples in Hadley's ggplot2 book often run on dsmall. - Mike Wise

2 Answers

2
votes

In R, you can specify parameters via names or not. When you specify without names, R lazily evaluates them with positional matching. You're getting an environment error due to the unnamed parameters not matching.

In this case, you need to specify the data first or use named parameters for everything:

library(ggplot2)
data(diamonds)
qplot(caret, price, data= diamonds, colour= color)
  Error in eval(expr, envir, enclos) : object 'caret' not found

qplot(data= diamonds, x= carat, y= price, colour= color) # works
qplot(data= diamonds, carat, price, colour= color) # also works
2
votes

Always try to work with a minimal working example and state whether or not the code you're presenting is complete or you're leaving something out.

From your question it is hard to know if what you've pasted is all there is to your code or if you have tried something else.

The line qplot(carat, price, data = dsmall, colour = color) does four things:

  1. It invokes qplot and tells it to plot some data.
  2. Tells qplot to plot the values in the carat column of the provided dataset against the values in the price column from the same dataset.
  3. Provides a symbol dsmall as data source for qplot to use. dsmall must be defined by the time qplot is invoked.
  4. Specifies that the color column in the provided dataset should be used to determine the color of the plotted symbols.

Now, R is telling you Error in eval(expr, envir, enclos) : object 'color' not found. This means, the column color hasn't been found in the provided datasource. This leads me to believe that you haven't appropriately prepared the dsmall datasource and hence you're having problems with the plot.

Try running the following code as provided in your console:

library(ggplot2)
set.seed(1410)
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
head(dsmall)
qplot(carat, price, data = dsmall, colour = color)

Resulting plot