Barplots, boxplots or normality plots are displayed according to the nature of described variable. These plots are useful to explore visually whether a continuous variable follows a normal distribution or to identify possible outliers or rare categories, etc.

knitr::opts_chunk$set(comment="",message=FALSE,warning=FALSE)

Step 1. Install the package

Install compareGroups package from CRAN and then load it by typing:

install.packages("compareGroups")
library(compareGroups)
library(compareGroups)

Step 2. Load data

Load the REGICOR example data available in compareGroups package:

data(regicor)

Setp 3. Computations

First use compareGroups function to store all values used to perform plots afterwards.

res <- compareGroups(year ~ .  , data = regicor)


You can use varinfo function to recover the original name of variables (not labels which are displayed in the results).

varinfo(res)

Step 4. Perform plots

by using the plot method which takes the results created by compareGroups function. Inside "[" brackets you can select which variable to plot. And, indicating bivar=TRUE a bivariate plot is performed, i.e. stratifying by groups.

a. For categorical variables a barplot is performed, stratifying by groups (right plot) or not (left plot):

plot(res['sex'])
plot(res['sex'], bivar=TRUE)

b. For continuous variables boxplots or normality plots are performed depending whether groups are considered or not, respectively.

plot(res['bmi'])
plot(res['bmi'],bivar=TRUE)


isubirana/compareGroups documentation built on Sept. 15, 2024, 7:46 a.m.