program 13

Author

JAISHREE-1NT23IS088

Program 13

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

step 1:load the libraries

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: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
#table(ToothGrowth$supp)
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

step 3: visualizing dot plot graph using ggplot and geom_dotplot

ggplot(ToothGrowth,aes(x=dose,y=len,color=supp,fill='pink'))+
  geom_dotplot(
     binaxis = "y",
     stackdir = "center",
     position = position_dodge(width = 0.8),
     dotsize=0.6,
     binwidth=1.5
     
     
  )+
  labs(
    title="Dot Plot of tooth length by dose and supplement type",
    x="Dose(mg/day)",
    y="Tooth length",
    color="supplement type"
  )+
  theme_minimal()