Reputation: 21
I'm a newbie, so this might be super basic. When I run this code
Dplot <- qplot(x = diamonds$carat, y = diamonds$price, color = diamonds$color) +
ggtitle("An A+ plot") +
xlab("carat") +
ylab("price") +
geom_point()
Dplot <- Dplot + facet_wrap(vars(diamonds$clarity))
Dplot
I get an error message that says:
Error in gList(list(x = 0.5, y = 0.5, width = 1, height = 1, just = "centre", : only 'grobs' allowed in "gList"
I've tried googling, but haven't been able to figure out what the issue is.
Upvotes: 1
Views: 310
Reputation: 146110
I would advise against using qplot
except in the most basic cases. It teaches bad habits (like using $
) that should be avoided in ggplot
.
We can make the switch by passing the data frame diamonds
to ggplot()
, and putting the mappings inside aes()
with just column names, not diamonds$
. Then the facet_wrap
works fine as long as we also omit the diamonds$
:
Dplot = ggplot(diamonds, aes(x = carat, y = price, color = color)) +
ggtitle("An A+ plot") +
xlab("carat") +
ylab("price") +
geom_point()
Dplot + facet_wrap(vars(clarity))
Dplot + facet_wrap(~ clarity) # another option
Notice the code is actually shorter because we don't need to type diamonds$
all the time!
The vars(clarity)
option works fine, more traditionally you would see formula interface used ~ clarity
. The vars()
option is new-ish, and will play a little nicer if you are writing a function where the name of a column to facet by is stored in a variable.
Upvotes: 3