Can geom_vline be connected across facet_grid?

109 views Asked by At

I am using geom_facet to visualize data across different samples. I calculate a set of percentiles beforehand for every sample and supply these values to geom_vline via a separate tibble. This allows to plot the percentiles of every sample correctly. However, for our purpose, I would like to have the vertical lines connected across the plots. A co-worker of mine is mapping the percentiles as geom_point and geom_line across the function geom_density_ridges(), but this plotting routine is very slow compared to facet_grid, so I would like to reproduce a similar look as seen on the right-hand picture here. Thank you so much for any advice as how to connect the geom_vlines:

enter image description here

Edit: Here is the code to create the lefthand plots: First create a tibble to supply the pecentiles for the vlines:

percentiles.tibble.facetplotting <- data %>%
         group_by(Batch) %>%
         summarize(p60 = unique(p60),
                   p80 = unique(p80),
                   p85 = unique(p85),
                   p90 = unique(p90),
                   p95 = unique(p95),
                   p99 = unique(p99)
                              )

Then use geom_area and facet_grid to plot:

 ggplot(data  , aes( x= data[[channel]] )  ) +
    facet_grid(Batch ~ . , scales = "free_y", switch = "y")+ # this brings the facet tag to the left
      
    geom_area( alpha = 0.15 ,   color = 'black', fill = 'grey',
              stat = "bin",
               binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)), 
             size = 0.5    )+
    
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p60), colour = "#a361c7"   ) +
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p80), colour = "#5ba962"   ) +
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p85), colour = "#c75a87"   ) +
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p90), colour = "#ab973d"   ) +
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p95), colour = "#648ace"   ) +
    geom_vline(data=percentiles.tibble.facetplotting, aes(xintercept = p99), colour = "#cb6342"   )+
    
        
    theme_minimal() +
    theme(axis.title.y = element_blank(), #take "count" away
      
      strip.text.x = element_text(size=15,  face="bold"),
      strip.background = element_rect(colour="black", fill="grey90", size=1, linetype="solid"),
      strip.text.y.left = element_text(angle = 0), # and once the tag is on the left, keep it horizontal
      panel.border=element_blank(), 
      axis.line=element_line(), # turn off the x-axis line on the bottom of the facet
        
          panel.spacing = unit(1.2, "lines"),
      
     
    )

The graph on the right hand side is a code written by my co-worker and uses:

      ggplot(temp_set, aes(x=temp_set[, channel], y=sample_name))+
        geom_density_ridges(fill='grey', scale = 1, alpha = 0.4, quantile_lines = FALSE, quantiles = c(0.6, 0.8, 0.85, 0.90, 0.95, 0.99))+
        geom_line(data=temp_set, mapping=aes(x=p60, group = 1, color= 'p60'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p60, group = 1), color= 'black' )+
        geom_line(data=temp_set, mapping=aes(x=p80, group = 1, color= 'p80'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p80, group = 1), color= 'black' )+
        geom_line(data=temp_set, mapping=aes(x=p85, group = 1, color= 'p85'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p85, group = 1), color= 'black' )+
        geom_line(data=temp_set, mapping=aes(x=p90, group = 1, color= 'p90'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p90, group = 1), color= 'black' )+
        geom_line(data=temp_set, mapping=aes(x=p95, group = 1, color= 'p95'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p95, group = 1), color= 'black' )+
        geom_line(data=temp_set, mapping=aes(x=p99, group = 1, color= 'p99'), orientation = 'y', size=1, alpha=0.7)+
        geom_point(data=temp_set, mapping=aes(x=p99, group = 1), color= 'black' )

0

There are 0 answers