PROGRAM 13

Author

spandana vr(1nt23is215)

13.Write an R program to create many dotplots from grouped data.Comparing the distributions of variables across using ggplot2’s position_dodge::function.

step1:load the necessary 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
data("ToothGrowth")

step2:explore dataset

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$dose)

0.5   1   2 
 20  20  20 
class(ToothGrowth$dose)
[1] "numeric"

step 3:converting dose column

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth,aes(x=dose,y=len,color=supp))+
  geom_dotplot(binaxis="y",#The axis to bin along, "x" (default) or "y"
               stackdir="center",#which direction to stack the dots. "up" , "down", "center", "centerwhole"
               position=position_dodge(width=0.8),
               dotsize=0.8,#The diameter of the dots relative to binwidth, default 1.
               binwidth=1.5)+ #controls spacing of dots on y axis??
  labs(title="tooth length by dose and suppliment type",
       x="Dose",
       y="length",
       color="supplement")+
  theme_minimal()