program 13 prac

Author

Gagana L

Write an R program to create a multiple dot plot or grouped data comparing the distrubution of various across different categories using ggplot2’s position_dodge function.

step 1:- load the required libary

library(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
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 2:- Convert dose to a factor

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

step 3:- create the dot plot

ggplot(ToothGrowth, aes(x = dose, y = len, color = supp)) +
  geom_dotplot(
    binaxis = "y",
    stackdir = "center",
    dotsize = 0.6,
    position = position_dodge(0.8),
    binwidth = 1.5
  ) +
  labs(
    title = "Tooth Growth by Supplement Type and Dose",
    x = "Dose (mg)",
    y = "Tooth Length"
  ) +
  theme_minimal()