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.
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()