Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R ggplot label number of observations per ordered violin with facet wrap

I’ve got a plot that looks like the output of the following code using the iris data

require(tidyverse)
require(purrr)
require(forcats) # Useful for ordering facets found at [here][1]

# Make some long data and set a custom sorting order using some of t
tbl <- iris %>% 
       pivot_longer(., cols = 1:4, names_to = "Msr", values_to = "Vls") %>%
       mutate(Msr = factor(Msr)) %>% 
       mutate(plot_fct = fct_cross(Species, Msr)) %>%                              
       mutate(plot_fct = fct_reorder(plot_fct, Vls))                           


# A functioning factory for minor log breaks found [here][1] (very helpful)
minor_breaks_log <- function(base) {
  # Prevents lazy evaluation
  force(base) 
  # Wrap calculation in a function that the outer function returns
  function(limits) {
    ggplot2:::calc_logticks(
      base = base, 
      minpow = floor(log(limits[1], base = base)), 
      maxpow = ceiling(log(limits[2], base = base))
    )$value
  }
}


# Plot the images
ggplot(data = tbl, aes(x =plot_fct, y = Vls, fill = Species)) +
       geom_violin() +
       coord_flip() +                                                               # swap coords
       scale_y_log10(labels = function(x) sprintf("%g", x),
                     minor_breaks = minor_breaks_log(10)) +                       # format for labels                                           # box fills
       theme_bw(base_size = 12) +
       annotation_logticks(base = 10, sides = "b") +
       facet_wrap(~Species, nrow = 1, scales = "free")  

Violin plots

I would now like to list the number of observations per violin on the right side of each facet just inside the maximum border, which I’m sure is possible but cannot seem to find an example that does this sort of labeling, with violins and facets.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ggplot(data = tbl, aes(y = plot_fct, fill = Species)) +
  geom_violin(aes(x = Vls)) +
  geom_text(aes(label = after_stat(count)), hjust = 1,
            stat = "count", position = "fill") +
  scale_x_log10(labels = function(x) sprintf("%g", x),
                minor_breaks = minor_breaks_log(10)) +                       # format for labels                                           # box fills
  theme_bw(base_size = 12) +
  annotation_logticks(base = 10, sides = "b") +
  facet_wrap(~Species, nrow = 1, scales = "free")  

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading