8

I'm learning ggplot2 and I don't understand why this doesn't work :

p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
     geom = "point",
     stat = "identity"
)
p
Error in as.environment(where) : 'where' is missing

Do you know why?

0

2 Answers 2

7

I think the problem is that you haven't specified what to use for the y-values. ggplot2 doesn't have the same default as the base graphics for plotting points against their index values. To use geom_point() with stat="identity" you'd need something like:

p<-ggplot(diamonds, aes(x=carat, y=cut))
p+layer(geom="point", stat="identity")

or more commonly

p+geom_point(stat="identity")

or however else you want try to plot your data.

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

Comments

2

Generally you don't use layer to build up a plot. Instead, you use geom or stat. p + geom_point() will plot what you're looking for. I'd suggest working through some of the examples in the gplot2 documentation.

2 Comments

right but by using layer I just wanted to understand what exactly are geom and stat and how they interact
Moving your data and aesthetics into the layer call gives you a proto object back, but I don't know what to do with it: layer(data=diamons, aes(x=caret), geom='point', stat='identity'). However, stat='identity' is rather redundant here.

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.