Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Assuming you have a dataset with two columns: sex and diagnosis level, you can use the following code to create either a boxplot or a barplot:

Load the required libraries

library(ggplot2)

Create a sample dataset

sex <- c("Male", "Male", "Male", "Female", "Female", "Male", "Female", "Female", "Male", "Male") diagnosis <- c("Level 1", "Level 2", "Level 1", "Level 1", "Level 2", "Level 1", "Level 2", "Level 2", "Level 2", "Level 1") df <- data.frame(sex, diagnosis)

Create a boxplot

ggplot(df, aes(x = diagnosis, y = sex)) + geom_boxplot() + ylab("Sex") + xlab("Diagnosis Level")

Create a barplot

ggplot(df, aes(x = diagnosis, fill = sex)) + geombar(position = "dodge") + ylab("Frequency") + xlab("Diagnosis Level") + scalefill_manual(values = c("blue", "pink"), name = "Sex")