-2

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

Eff
  • 141
  • 1
  • 8

1 Answers1

3

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

Quinten
  • 35,235
  • 5
  • 20
  • 53