Program 12

Author

Tejaswini Reddy U

  1. 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

library(ggplot2)

Step2- Create the violin plot

violin_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  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)