PROGRAM 13

Author

PRACHETAN MS

Write an r program to create multiple dot plots for grouped data comparing the distributions of variables across different categories using ggplot2 lib.

Objective

To create multiple dot plots for grouped data and compare the distribution of tooth length across different supplement types and dosage levels using ggplot2 and dplyr.

Step 1: Load required packages

We use ggplot2 for visualization and dplyr for data manipulation.

#install.packages("ggplot2") # Uncomment if needed
#install.packages("dplyr")  # Uncomment if needed

library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.3
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Step 2: Use an inbuilt dataset

We will use the built-in ToothGrowth dataset. It contains:

# len → Tooth length
# supp → Supplement type (VC or OJ)
# dose → Dosage level
# Load and preview dataset
data(ToothGrowth)
head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
str(ToothGrowth)
'data.frame':   60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

Step 3: Data preprocessing

Convert categorical variables into factors for better grouping.

ToothGrowth <- ToothGrowth %>%
  mutate(
    supp = as.factor(supp),
    dose = as.factor(dose)
  )

Step 4: Create basic dot plot

We first create a simple dot plot of tooth length grouped by supplement type.

ggplot(ToothGrowth, aes(x = supp, y = len))

ggplot(ToothGrowth, aes(x = supp, y = len, color = supp)) +
  geom_dotplot(
    binaxis = "y",
    stackdir = "center",
    dotsize = 0.8
  )
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

Step 5: Create multiple dot plots using facets

We now create multiple dot plots based on dosage levels.

ggplot(ToothGrowth, aes(x = supp, y = len, color = supp)) +
  geom_dotplot(
    binaxis = "y",
    stackdir = "center",
    dotsize = 0.8
  ) +
  facet_wrap(~ dose)
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

Step 6: Add labels and theme

Enhance the plot with titles and clean styling.

ggplot(ToothGrowth, aes(x = supp, y = len, color = supp)) +
  geom_dotplot(
    binaxis = "y",
    stackdir = "center",
    dotsize = 0.8
  ) +
  facet_wrap(~ dose) +
  labs(
    title = "Multiple Dot Plots of Tooth Growth",
    subtitle = "Comparison across supplement types and dosage levels",
    x = "Supplement Type",
    y = "Tooth Length"
  ) +
  theme_minimal()
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.