5

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?

4

2 回答 2

8

this:

ggplot(d) + geom_boxplot(aes(factor(group), x))

gives the following plot

enter image description here

于 2013-04-22T13:15:58.463 回答
5

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.

于 2013-04-22T13:15:01.723 回答