Converting numbers into categorical data for violin plot

Hello.
I have a csv dataset which has labelled males and females as integers (1 and 2 respectively). I'm struggling to convert these numbers into discrete categories rather than continuous integers in order for my violin plot to display two separate plots for males and females rather than a single plot with 1, 1.25, 1.5 etc.. on the x axis.

I also need to layer a box plot on top of both these violin plots, and I'm not so sure how to do this either. I'm using ggplot2

I'm sure it's a simple-ish solution but I'm new to R so could use some help.

Thanks!

Hi,

Welcome to the RStudio community!

All you need to do is convert the 1 and 2 into factors. You can either do this by explicitly converting them using the as.factor() function, or you can use something like ifelse() as I have done below to change the numbers into names.

library(ggplot2)

set.seed(1) #Only needed for reproducibility 
df = data.frame(
  id = 1:10,
  gender = sample(1:2, 10, replace = T),
  val = runif(10)
)

df$gender = ifelse(df$gender == 1, "male", "female")

ggplot(df, aes(x = gender, y = val)) + 
  geom_violin()

Created on 2022-04-01 by the reprex package (v2.0.1)

Hope this helps,
PJ

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.