Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this command in ggplot is returning an error?

Tags:

r

layer

ggplot2

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?

like image 549
Wicelo Avatar asked Jun 25 '13 15:06

Wicelo


2 Answers

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.

like image 190
mme Avatar answered Sep 23 '22 03:09

mme


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.

like image 29
Justin Avatar answered Sep 19 '22 03:09

Justin