Program-13

Author

Stuti Shamsundar Kulkarni

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

Step-1 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")

Step-2: 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
class(ToothGrowth$supp)
[1] "factor"
table(ToothGrowth$dose)

0.5   1   2 
 20  20  20 

Step-3: Converting dose column

# if a column is converted into factor, it is easy to group
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose , y= len, color = supp))+
  geom_dotplot(binaxis = "y", #The axis to bin along 
               stackdir = 'center', #which direction to stack the dots.
               position = position_dodge(width = 1.5), #Dodging preserves the vertical position of an geom while adjusting the horizontal position
               dotsize = 0.8)+
  labs(title = "Tooth length by dose and suppliment type",
       x= "Dose",
       y=" length",
       color = "suplement")+
  theme_minimal()
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.