1

Using Dr. Evers suggestion to shade areas under a density curve with ggridges works well. However, I find density curves can be deceiving in that they imply data is there when it's not. Thus, I thought I'd try this shading technique with an ordinary histogram.

However, when I try to use it with a histogram the shading is a little off. Why is this?

library(tidyverse)
install.packages("ggridges", dependencies=TRUE)  # there are many
library(ggridges)
 t2 <-   structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04", 
"1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10", 
"1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04", 
"1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10", 
"1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9, 
18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8, 
22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853", 
"1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853", 
"1853", "1854", "1854", "1854", "1854", "1854", "1854", "1854", 
"1854", "1854", "1854", "1854", "1854")), row.names = c(NA, -24L
), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date", 
"t", "year"))


gg <- ggplot(t2, aes(x = t, y = year)) +
      geom_density_ridges(stat = "binline", bins = 10, scale = 0.8, 
                      draw_baseline = TRUE) +
      theme_ridges()

# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]

# Add geom_ribbon for shaded area
gg +
  geom_ribbon(
    data = transform(subset(d, x >= 10), year = group),
    aes(x, ymin = ymin, ymax = ymax, group = group),
    fill = "red",
    alpha = 1.0) 

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
ixodid
  • 2,180
  • 1
  • 19
  • 46

2 Answers2

1

There is indeed something strange going on. Please see below for a "conclusion".

  1. If we plot gg only:

     gg;
    

    enter image description here

  2. If we plot gg plus a stairstep which should correspond to the trace of gg:

      gg +
          geom_step(
              data = d, 
              aes(xmax, ymax, group = group), 
              direction = "vh", col = "red",  size = 2);
    

enter image description here

So adding geom_step somehow changes gg. I don't understand how this could be. You can see that geom_step (the red curve) does in fact correspond to the trace of the histogram when plotting gg by itself (see first plot).

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • I have posted this issue at https://github.com/clauswilke/ggridges/ and will circle back here if/when resolved. – ixodid Apr 24 '18 at 20:59
  • The bin line changes because the x axis limits change between the first and the second plot and the histogram was specified only by number of bins, not by binwidth and location. Either fixing the x axis limits or the binwidth and location [resolves the issue](https://stackoverflow.com/a/50011074/4975218). – Claus Wilke Apr 24 '18 at 22:00
1

The following works if you're willing to resize and move the bins so a bin boundary lies exactly at your demarcation line (here 10).

ggplot(t2, aes(x = t, y = year, fill = ifelse(..x..>=10, ">= 10", "< 10"))) +
  geom_density_ridges_gradient(stat = "binline", binwidth = 3,
                               center = 8.5, scale = 0.8, 
                               draw_baseline = TRUE) +
  theme_ridges() +
  scale_fill_manual(values = c("gray70", "red"), name = NULL)

enter image description here

The reason why you observe the effect you do is because the x axis changes between the first and the second plot, and the x axis range has an effect on how the bins are drawn. There are two solutions: You can either fix the x axis range or define the bins via center and binwidth rather than bins. (The second option is preferred always anyways, in my opinion, regardless of how you treat the x axis.)

First, fix the x axis range:

gg <- ggplot(t2, aes(x = t, y = year)) +
  geom_density_ridges(stat = "binline", bins = 10, scale = 0.8, 
                      draw_baseline = TRUE) +
  theme_ridges() +
  scale_x_continuous(limits = c(-12, 28)) # this is where the change is

# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]

# Add geom_ribbon for shaded area
gg +
  geom_ribbon(
    data = transform(subset(d, x >= 10), year = group),
    aes(x, ymin = ymin, ymax = ymax, group = group),
    fill = "red",
    alpha = 1.0) 

enter image description here

Second, the alternative bin definition:

gg <- ggplot(t2, aes(x = t, y = year)) +
  geom_density_ridges(stat = "binline",
                      binwidth = 3, center = 8.5, # this is where the change is
                      scale = 0.8, draw_baseline = TRUE) +
  theme_ridges()

# Build ggplot and extract data
d <- ggplot_build(gg)$data[[1]]

# Add geom_ribbon for shaded area
gg +
  geom_ribbon(
    data = transform(subset(d, x >= 10), year = group),
    aes(x, ymin = ymin, ymax = ymax, group = group),
    fill = "red",
    alpha = 1.0) 

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104