Swathi_1nt23is228

Write an R program to create multiple dot plots or group data , comparing the distributions of variables across different categories, using ggplot2’s position_dodge function.

Step1: Load the library

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

Step2: Explore the data-set

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 ...
p=ToothGrowth

Step 3: Create a dot plot

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ToothGrowth$dose
 [1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 1   1   1   1   1   1   1   1   1  
[20] 1   2   2   2   2   2   2   2   2   2   2   0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
[39] 0.5 0.5 1   1   1   1   1   1   1   1   1   1   2   2   2   2   2   2   2  
[58] 2   2   2  
Levels: 0.5 1 2
ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) 

ggplot(ToothGrowth, aes(x=dose, y=len, color=supp))+ 
  geom_dotplot(
    binaxis = 'y',  #used to group many entries into separate bins, x means group                      vertically , y means group horizontally
    stackdir = 'center', #which direction to stack the dots
    position = position_dodge(width = 0.8),
    dotsize = 0.6,
    binwidth = 1.5
  ) + labs(title = "Dotplot of Tooth Length and Dosage",
              x="Dose",
              y="Len")+ theme_minimal()