# install.packages("ggplot2")
library(ggplot2)Program_13
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.
Step 1: Install and load ggplot2
Step 2: Load 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: Create dot plot with grouping
ggplot(ToothGrowth, aes(x = factor(dose), y = len, color = supp)) +
geom_dotplot(
binaxis = "y",
stackdir = "center",
position = position_dodge(width = 0.7),
binpositions = "all",
dotsize = 1.0
) +
labs(
title = "Tooth Growth by Dose and Supplement Type",
x = "Dose",
y = "Tooth Length",
color = "Supplement"
) +
theme_minimal()Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.