Search code examples
rdata-visualization

Violin plot in R, each column a violin, split by gender


I have a data structure where each row represents an individual, and each column represents something about that individual.

I want to plot multiple violins in a single violin plot, where each violin represents a column in my data matrix. But I want each column to be split into two groups, one for male and one for female. I need to be able to do it for ~10 columns.

For example, if we consider the following simple data matrix:

v1 v2 v3 v4 Sex
24 32 84 76 1
12 17 23 98 2
.. .. .. .. .
.. .. .. .. .
34 33 56 87 2
14 42 66 92 1

I want to plot v1, ..., v4 each as a violin split into two by sex. Something like illustrated below. How can I achieve this?enter image description here


Solution

  • You can use the function geom_split_violin from the introdataviz package mentioned in this question. First convert your data to longer format like this:

    df <- read.table(text = "v1 v2 v3 v4 Sex
    24 32 84 76 1
    12 17 23 98 2
    34 33 56 87 2
    14 42 66 92 1", header = TRUE)
    
    library(tidyr)
    library(dplyr)
    library(ggplot2)
    # devtools::install_github("psyteachr/introdataviz")
    library(introdataviz)
    df %>%
      pivot_longer(cols = -Sex) %>%
      mutate(Sex = factor(Sex)) %>%
      ggplot(aes(x = name, y = value, fill = Sex)) +
      geom_split_violin()
    

    Created on 2022-09-03 with reprex v2.0.2