Search code examples
rggplot2facet-wrap

facet_wrap not showing the second facet with geom_density_ridges


I am trying to subset parts of a data you can download the csv from here and read it as datplot. This is what its head looks like:

> head(datplot)
# A tibble: 6 × 3
  bta   electrode  value
  <chr> <chr>      <dbl>
1 b0    Fz         3.03 
2 b0    Cz         1.78 
3 b0    Pz        -1.05 
4 b0    Fz         3.78 
5 b0    Cz         2.82 
6 b0    Pz        -0.242

As you can see, the data has a variable named bta with levels "b0" and "b1" and values from a particular distribution. What I am trying to do, is wrap the two facets but I can't manage to do it.

This is the ggplot code I am using at the moment:

time <- "225"
col <- c("#004d8d", "#cc2701", "#e5b400")

p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
    geom_density_ridges(data= subset(datplot, bta='b0'), scale = -0.5, alpha=0.2, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha("white", 0.3), aes(fill = electrode)) +
    #plotb1
    geom_density_ridges(data= subset(datplot, bta='b1'),scale = -0.5, alpha=0.5, show.legend = FALSE,
                         quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                         vline_color = alpha("white", 0.6), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +   
    scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
    labs(title=sprintf('%s ms.',time)) +
    ylab("Electrode") +
    xlab(TeX(r'(Signal (µV) Posteriors   $\beta_1\cdot x\; (x=1)$)')) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
  p1

This code results only with the plot corresponding to "b0" but the other facet seems to be missing, as you can see in the following image:

incomplete facet plot

And for some reason, the data that corresponds to "b1" does not appear. I am probably failing in something with the code that I am unsuccesful identifying. In case it helps, the missing plot, which I plotted alone, would look something like this:

b1 plot alone

So clearly something is failing when using both geom_density_ridges together in the previous code.

[UPDATE]

Following a comment by @Paul, I tried to not subset the data with the following code:

p1 <- datplot %>% ggplot(mapping = aes(x=value, y=factor(electrode, level = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
    scale_y_discrete() +
    geom_rect(data=data.frame(), inherit.aes = FALSE, mapping = aes(
      ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)), fill = "black", alpha = 0.1) +
    geom_density_ridges(data= datplot, scale = -0.5, alpha=0.5, show.legend = FALSE,
                        quantile_lines = TRUE, quantiles = c(0.025, 0.5, 0.975), 
                        vline_color = alpha("white", 0.3), aes(fill = electrode)) +
    facet_wrap(~bta) +
    scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +   
    scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
    labs(title=sprintf('%s ms.',time)) +
    ylab("Electrode") +
    xlab(TeX(r'(Signal (µV) Posteriors   $\beta_1\cdot x\; (x=1)$)')) +
    theme_light() +
    theme(axis.text = element_text(size = 14)) +
    theme(axis.title = element_text(size = 16)) +
    theme(plot.title = element_text(size = 20)) +
    coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
  p1

With the same output:

without subset

Thanks for the help!


Solution

  • Attempted a minimal reproducible example:

    library(ggplot2)
    library(ggridges)
    library(magrittr)
    
    # data for testing
    datplot <- data.frame(
      bta = rep(c("b0", "b1"), each = 75),
      electrode = rep(c("Fz","Cz","Pz"), times = 50),
      value = runif(150,-2,5)
    )
    
    time <- "225"
    col <- c("#004d8d", "#cc2701", "#e5b400")
    
    p1 <- datplot %>%
      ggplot(mapping = aes(x=value, y=factor(electrode, levels = c('Fz','Cz','Pz')), group = electrode, color = electrode)) +
      scale_y_discrete() +
      geom_density_ridges(scale = -0.5, alpha=0.2,
                          quantile_lines = TRUE, 
                          quantiles = c(0.025, 0.5, 0.975), 
                          vline_color = alpha("white", 0.3), 
                          aes(fill = electrode)) +
      facet_wrap(~bta) +
      coord_flip(xlim = c(-8, 8), ylim = c(0.4,3.05), expand = FALSE, clip = "on")
    p1
    

    This produces a faceted plot which I think meets the meat of the request - there is much to be done to match desired aesthetics but this should provide a start point from which you can tweak the appearance.