Home > Blockchain >  R: how to add diagonal line to binned boxplots in ggplot
R: how to add diagonal line to binned boxplots in ggplot

Time:04-29

# library
library(ggplot2)
library(dplyr)

# Start with the diamonds dataset, natively available in R:
p <- diamonds %>%
  # Add a new column called 'bin': cut the initial 'carat' in bins
  mutate(bin=cut_width(carat, width = 0.5, boundary=0) ) %>%
  # plot
  ggplot(aes(x=bin, y= x) )  
  geom_boxplot()  
  xlab("Carat")   geom_abline(slope = 1, intercept = 0)
p

I tried using geom_abline to add a 45-degree diagonal line. This produces a black line. However, this does not exactly match with the bin on the x-axis. For example, when bin = (2.5,3], the black line's y-coordinate is at 6.

I roughly drew (in blue) where I think the 45-degree diagonal line should be. For example, for bin = (2.5, 3], the y-coordinate should 2.75 (mid-point of the bin). For bin = (3, 3.5], the y-coordinate should be 3.25 (mid-point of the bin). Is there a way to produce this line in ggplot?

enter image description here

CodePudding user response:

For ggplot, any categories on an axis have a distance of 1. So geom_abline with a slope of 1 will increase your y-axis with 1 for each category. Since your bins are of size 1/2, using a slope of 0.5 will draw the slope correctly.

We also need to adjust the intercept to -0.25. This is because the first bin is at x-coordinate 1, not 0.25.

p <- diamonds %>%
    # Add a new column called 'bin': cut the initial 'carat' in bins
    mutate(bin=cut_width(carat, width = 0.5, boundary=0) ) %>%
    # plot
    ggplot(aes(x = bin, y = x))  
    geom_boxplot()  
    xlab("Carat")   
    geom_abline(slope = 0.5, intercept = -0.25)   
    geom_hline(yintercept = c(2.75, 3.25))

Note that I also drew 2 horizontal lines to confirm that this fits your manually worked out example values.

enter image description here

  • Related