program 13

Author

srushtivh 1nt23is218

Write a R program to create many dotplots from the grouped data.comparing the distribution of varaiables across using ggplot2’s dodge position 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
data("ToothGrowth")

step2:load datset

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"
summary(ToothGrowth)
      len        supp         dose      
 Min.   : 4.20   OJ:30   Min.   :0.500  
 1st Qu.:13.07   VC:30   1st Qu.:0.500  
 Median :19.25           Median :1.000  
 Mean   :18.81           Mean   :1.167  
 3rd Qu.:25.27           3rd Qu.:2.000  
 Max.   :33.90           Max.   :2.000  
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth,aes(x=dose,y=len,color=supp))+
  geom_dotplot(binaxis = "y",
               stackdir = "center",
#which direction to stack the dots. "up" (default), "down", "center", "centerwhole" (centered, but with dots aligned)
               position = position_dodge(width = 0.8),
               dotsize = 0.8,   
#The diameter of the dots relative to binwidth
               binwidth = 1.5)+#controls spacing of dots on y axis
  labs(title="tooth length dose by suppliment type",
       x = "Dose",
       y ="length",
       color="supplement")+
  theme_minimal()