ggplot2 facet_wrap doesn't find a variable but shape does

4.3k views Asked by At

I'm running in a bit of a problem plotting some data with ggplot2: I want to use a facet_wrap over a variable AdultInputProp, but R doesn't find the variable and instead returns an Error in as.quoted(facets) : object 'AdultInputProp' not found. Now I understand that this simply means that R can't find this variable in the dataset used to plot, but if I ask ggplot2 to instead use the same variable for to create a shape scale, it works just fine. Any idea what the problem might be?

Sorry, I'm not too sure how to make a minimal working example with a generated df from scratch, so here's the df I'm using, and the code bellow. I've also tried using facet_grid instead of facet_wrap but ran into the same problem.

The code here with facets returns the above-mentioned error:

df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
                            aes(x=TestIteration, y=Error,
                                colour=GoalBabblingProp,
                                group=interaction(GoalBabblingProp,
                                                  AdultInputProp))) +
  facet_wrap(AdultInputProp) +
  xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
  scale_colour_discrete(name = "Goal babbling proportion") +
  geom_line(position = position_dodge(1000)) +
  geom_errorbar(aes(ymin=Error-ci,
                    ymax=Error+ci),
                color="black", width=1000,
                position = position_dodge(1000)) +
  geom_point(position = position_dodge(1000),
             size=1.5, fill="white")

This other code, exactly the same except for the facet_wrap line deleted and with shape added works fine:

df.plot.GBPperAIP <- ggplot(df.sum.GBPperAIP,
                            aes(x=TestIteration, y=Error,
                                colour=GoalBabblingProp,
                                shape=AdultInputProp,
                                group=interaction(GoalBabblingProp,
                                                  AdultInputProp))) +
  xlab("Step") + ylab("Mean error") + theme_bw(base_size=18) +
  scale_colour_discrete(name = "Goal babbling proportion") +
  geom_line(position = position_dodge(1000)) +
  geom_errorbar(aes(ymin=Error-ci,
                    ymax=Error+ci),
                color="black", width=1000,
                position = position_dodge(1000)) +
  geom_point(position = position_dodge(1000),
             size=1.5, fill="white")
1

There are 1 answers

1
Jason Punyon On BEST ANSWER

facet_wrap expects a formula, not just a naked variable name. So you should change it to

...
facet_wrap(~ AdultInputProp) +
...