Dot Plot

Author

Manoj

Write an R program to create multiple dot plots for grouped data, comparing the distributions of varialbes across different categories, using ggplot2 lib

Steps

  • Step 1: Install and load required packages
  • Step 2: Load the dataset (built-in)
  • Step 3: Explore dataset
  • Step 4: Use ggplot2 for visualization

Step 1: Install and load required packages

We will be using ggplot2 and dplyr package We should have installed these pakages before using them

install.packages(‘ggplot2’) install.packages(‘dplyr’)

library(ggplot2)
library(dplyr)
Warning: package 'dplyr' was built under R version 4.5.2

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

Step 2: Load the dataset (built-in)

we will be using ToothGrowth dataset for building dotplot

data=ToothGrowth

We shall see the first and last few rows of the dataset

head(data)
   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
tail(data)
    len supp dose
55 24.8   OJ    2
56 30.9   OJ    2
57 26.4   OJ    2
58 27.3   OJ    2
59 29.4   OJ    2
60 23.0   OJ    2
str(data)
'data.frame':   60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
summary(data)
      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  

Step 4: Use ggplot2 for visualization

Create multiple dot plots to compare the distribution of tooth length (len), across different supplement types (supp), and dosages (dose), using position_dodge group-wise seperation.

ggplot(data, aes(x=dose, y=len, color=supp))

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center'
  )
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    position=position_dodge(width=1)
  )
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    position=position_dodge(width=1),
    dotsize = 0.6,
    binwidth = 1.5
  )

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    position=position_dodge(width=1),
    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'
          )

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    position=position_dodge(width=1),
    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'
          )

ggplot(data, aes(x=dose, y=len, color=supp))+
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    position=position_dodge(width=1),
    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'
          )+theme_minimal()+
  theme(legend.position = 'top')