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?
We often convert continuous variables into discrete ones. We do this by dividing up the continuous variable into ranges of values. We then assign the same discrete value to all values of the continuous variable that fall within a certain range.
Discretization is the process through which we can transform continuous variables, models or functions into a discrete form. We do this by creating a set of contiguous intervals (or bins) that go across the range of our desired variable/model/function. Continuous data is Measured, while Discrete data is Counted.
You can use the cut() function in R to create a categorical variable from a continuous one. Note that breaks specifies the values to split the continuous variable on and labels specifies the label to give to the values of the new categorical variable.
To create the bins for a continuous vector, we can use cut function and store the bins in a data frame along with the original vector. The values in the cut function must be passed based on the range of the vector values, otherwise, there will be NA's in the bin values.
this:
ggplot(d) + geom_boxplot(aes(factor(group), x))
gives the following plot
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With