5

I seem to be having problems using ggplot2.

I get the following error while trying to plot box plots with the aes_string:

Error: stat_boxplot requires the following missing aesthetics: x, y

Here is an example:

x='group'
y='value'
df=data.frame(group=c('A','A','B','B','C','C'),value=c(1,2,3,4,5,6))
ggplot(data=df,aes_string(x,y)) + geom_boxplot() #returns the error
ggplot(data=df,aes(x,y)) + geom_boxplot() #plots nonsense (naturally)
ggplot(data=df,aes(group,value)) + geom_boxplot() #works, but not strings

Any suggestions on how I can make this work with strings?

1

1 Answer 1

7

aes allows the first two arguments to be unnamed and are assumed to be x and y (respectively); aes_string does not have this shortcut, and so all arguments must be named. Try:

ggplot(data=df,aes_string(x='group',y='value')) + geom_boxplot()
Sign up to request clarification or add additional context in comments.

3 Comments

The only question is - why?
ggplot(data=df, aes_string(x=x,y=y)) + geom_boxplot() also works.
aes allows the first two arguments to be unnamed and are assumed to be x and y (respectively); aes_string does not have this shortcut, and so all arguments must be named.

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.