user248237
user248237

Reputation:

binning geom_boxplot in ggplot2 in R?

I want to use geom_boxplot to make boxplots that correlate two variables: for each bin of x values, plot the distribution (as boxplot) of y values for that bin. I tried:

ggplot(cars) + geom_boxplot(aes(x=dist, y=speed))

but this creates basically one large bin of x values. How can I make it so for each bin of dist, there's a boxplot representing the corresponding speed values?

Upvotes: 3

Views: 6967

Answers (2)

cubic.inf
cubic.inf

Reputation: 73

Just to put it out there, you could also do

bin_size <- 10

cars %>% 
  mutate(bin_dist = factor(dist%/%bin_size*10)) %>% 
  ggplot(aes(x = bin_dist, y = speed)) +
  geom_boxplot()

geom_boxplot, bin-size 10

And to make the labeling better:

(cars2 <- cars %>% 
  mutate(bin_dist = dist%/%bin_size*10)) %>% 
  ggplot(aes(x = factor(bin_dist), y = speed)) +
  geom_boxplot() +
  scale_x_discrete(labels = paste0(unique(cars2$bin_dist), "-", unique(cars2$bin_dist)+10)) +
  labs(x = "dist")

Same plot with improved labels

cars2 gets saved so it can work in paste0.

Upvotes: 4

Ciar&#225;n Tobin
Ciar&#225;n Tobin

Reputation: 7536

Not sure what you mean by "bin", since you haven't provided any bins in your question. If you just mean that you would like a speed boxplot for each unique dist value, you can do it like this (treating dist as discrete):

ggplot(cars) + geom_boxplot(aes(factor(dist), speed))

If you were to actually create bins you could do something like:

cars$bin <- cut(cars$dist, c(1, 10, 30, 50, 200))
ggplot(cars) + geom_boxplot(aes(bin, speed))

Upvotes: 6

Related Questions