1

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?

3
  • 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. Commented Jan 21, 2016 at 20:54
  • 2
    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. Commented Jan 21, 2016 at 20:54
  • 1
    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. Commented Jan 21, 2016 at 21:09

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, man! I guess the missing part was the data(diamonds) thingy. Do I have to ALWAYS do that? My thinking was that since the dataset is inbuilt, R would be able to figure everything out well.
No. You almost never have to do it anymore. The default for packages is now to "lazyload" data, which means you almost never need to use data(), and indeed in this case it is unnecessary.
2

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

3 Comments

You are right. Thanks! I DID initialize dsmall, but like this: small <- diamonds[sample(nrow(diamonds), 100)]. I guess the error came from omitting the comma and space at the end. Am I right? And if I am, why is it so?
@ayePete. Read the chapter on subsetting (Chapter 2 I believe) in Hadley Wickams Advanced R book (or something similar). The book is online. Until you have grokked that you will ever find data.frame syntax mystifying.
Nice first post anatary. Very complete.

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.