Write an R program to create multiple dot plots for grouped data , comparing the distribution of variable across different using ggplot2’s position dodge function .
Step 1: Load the library
# Load the necessary librarylibrary(ggplot2)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
Step2 : Load the built-in dataset.
# Load the ToothGrowth datasetdata("ToothGrowth")str(ToothGrowth)
# Create the dot plotggplot(ToothGrowth, aes(x =factor(dose), y = len, color = supp)) +geom_dotplot(binaxis ="y", # Stack the dots along the y-axisstackdir ="center", # Center the dotsposition =position_dodge(width =0.8), # Separate dots by 'supp' using position dodgedotsize =0.6, # Adjust dot size for claritybinwidth =1.5# width of the bin ) +labs(title ="Dot Plot of Tooth Length by Dose and Supplement Type",x ="Dose(mg/day) ",y ="Tooth Length (mm)",color ="Supplement Type" ) +theme_minimal()