Search code examples
rggplot2binningcontinuous

Binning continuous variable then visualizing it with ggplot does not reproduce example in textbook?


Given "diamonds" dataset in tidyverse, and filtering it as per ch.7 in "R for Data Science":

smaller <- diamonds %>% filter(carat < 3),

I expect

ggplot(data = smaller, mapping = aes(x = carat, y = price)) + 
+   geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))

to return price vs carat (binned), but instead see this returned.

Why is this? Is it because of a change in ggplot2, or is it another reason?


Solution

  • Yes. With the release of ggplot2 3.3.0 bi-directional geoms and stats have been introduced, which also result in a kind of direction determination by ggplot2. See here:

    For this reason you have to add orientation=x to get the plot from R4DS:

    library(ggplot2)
    library(dplyr)
    
    smaller <- diamonds %>% filter(carat < 3)
    
    ggplot(data = smaller, mapping = aes(x = carat, y = price)) + 
      geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)), orientation = "x")
    

    Created on 2020-04-13 by the reprex package (v0.3.0)