Reputation: 8178
I have a variable which encodes group ID:
d <- data.frame(group = c(0,1,0,2,1,3,2,0,1,2), x=c(1.2,2.3,3.2,2.1,1.3,1.5,2.3,0.4,1.3,1.7))
When I try to use it in ggplot2 for making boxplots I get an error
Continuous value supplied to discrete scale
At attempt to render data. Then I manually change at least one group ID in data to text everything works OK.
So, my question is: is where some easy way to change continuous variable, containing finite number of variants to discrete?
Upvotes: 6
Views: 55334
Reputation: 8753
this:
ggplot(d) + geom_boxplot(aes(factor(group), x))
gives the following plot
Upvotes: 8
Reputation: 8976
Since you're providing the group
variable with a numeric vector, this is understood as a continuous variable. You need to convert it to a categorical variable. Try the following:
d <- data.frame(group = as.factor(c(0,1,0,2,1,3,2,0,1,2)), x=c(1.2,2.3,3.2,2.1,1.3,1.5,2.3,0.4,1.3,1.7))
The as.factor
function will convert the numeric vector you provided for the groups to a discrete variable.
Upvotes: 5