library(ggplot2)
Program 12
- Develop a script in R to create a violin plot displaying the distribution of a continuous variable, with separate violins for each group, using ggplot2.
Step 1: Load the libraries
Step2- Create the violin plot
<- ggplot(data = iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
violin_plot geom_violin(trim = FALSE) + # Show full distribution without trimming tails
geom_boxplot(width = 0.1, fill = "white") + # Add a boxplot inside violins
labs(title = "Distribution of Sepal Length by Species",
x = "Species",
y = "Sepal Length") +
theme_minimal() +
theme(legend.position = "none")
Step3- Display the plot
print(violin_plot)