program 13

Author

Varsha GB-1NT23IS243-D

13.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 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(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

step3:create dot plot

ToothGrowth$dose<-as.factor(ToothGrowth$dose)
ggplot(ToothGrowth,aes(x=dose,y=len,color=supp))+
  geom_dotplot(
    binaxis = 'y',   #The axis to bin ,x means group verticaly                            ,y means group horizontally
     stackdir ='center',    #which direction to stack the dots
    position = position_dodge(width=0.8),
    dotsize = 0.6,    #The diameter of the dots relative to                                binwidth
    binwidth = 1.5  #controls spacing of dots on y-axis
  )+
  labs (

title = "dot plot of tooth length by dose and supplement type",
x= "Dose(mg/day)",
y = "Tooth length",
color="Supplement Type"
)+
theme_minimal()