Program_13

Author

Anusha Y K

###Program: Write an R program to create multiple dot plots for grouped data, comparing the distributions of variables across different categories, using ggplot2’s position_dodge function.

1. Load ggplot2

library(ggplot2)

2. Prepare the iris data

df <- iris
df$Size_Category <- ifelse(df$Sepal.Width > 3, "Large Width", "Small Width")

3. Create the grouped dot plot

sepal_plot <- ggplot(df, aes(x = Species, y = Sepal.Length, fill = Size_Category)) +
  geom_dotplot(
    binaxis = "y", 
    stackdir = "center", 
    position = position_dodge(0.8), # This prevents the two 'Width' groups from overlapping
    dotsize = 0.6
  ) +
  labs(
    title = "Sepal Length Distribution by Species and Width Category",
    x = "Iris Species",
    y = "Sepal Length (cm)",
    fill = "Width Category"
  ) +
  theme_bw()

4. Display the plot

print(sepal_plot)
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.