Visualize data distribution

Numerical data

Violin plots

# Default parameters
x <- runif(10)
plot_violin(x)

# Multiple variables
df <- lapply(seq(2), function(x) runif(10))
df <- as.data.frame(df)
df[, 3] <- runif(10, 1, 2)
colnames(df) <- paste0("X", seq(3))
plot_violin(
    df,
    title = "Some numerical variables",
    colour = brewer.pal(9, "Set1")[seq(3)]
)


# Advanced parameters
plot_violin(
    df,
    title = "Some numerical variables",
    color_title = brewer.pal(9, "Set1")[5],
    ylab = "Y-values",
    colour = brewer.pal(9, "Set1")[seq(3)],
    method = "kruskal",
    method_adjust = "none",
    cex = 1.2,
    pch_size = 3,
    width_text = 5,
    pch_colour = "gray30",
    pch_alpha = 0.5,
    width_title = 30,
    lwd = 1.25,
    digits = 2
)

Histograms

# Default parameters
x <- rnorm(100)
plot_histogram(x)


# Advanced parameters
plot_histogram(
    x,
    title = "Some numerical variable",
    width_title = 15,
    color = "blue",
    color_title = "orange",
    color_stats = "orange",
    cex = 1.2,
    digits = 1,
    binwidth = 0.5
)

Barplots

# Default parameters
x <- runif(10, 1, 10) %>%
    set_names(paste("Sample", LETTERS[seq(10)]))
plot_bar(x)

# Advanced parameters
plot_bar(
    x = x,
    title = "Some numerical variable",
    width_title = 30,
    colour = c("yellow", "gray", "red"),
    color_title = "blue",
    cex = 1.2,
    digits = 1,
    n_max = 5,
    ratio = 15,
    hjust_title = 1
)

Categorical data

Piecharts for a single variable

# Default parameters
x <- c(rep("A", 5), rep("B", 4))
plot_pie(x)

# Advanced parameters
k <- 10
n <- runif(k, 1, 10) %>% round()
x <- paste("Level", seq(k)) %>%
    mapply(function(x, y) rep(x, y), ., n) %>%
    unlist()
plot_pie(
    x,
    title = "Some categorical variable",
    width_text = 20,
    width_title = 20,
    colour = rev(brewer.pal(9, "Reds")),
    cex = 20,
    digits = 1,
    hsize = 1.5,
    collapse = TRUE,
    b = 3
)

Barplots for multiple variables (or for a variable whose levels may be present simultaneously)

# Default parameters
df <- sapply(seq(10), function(x) runif(10) %>% round()) %>% as.data.frame()
colnames(df) <- paste("Level", seq(10))
plot_bar_mcat(df)

# Advanced parameters
plot_bar_mcat(
    df,
    sample_size = 15,
    title = "Some categorical variable",
    width_text = 30,
    width_title = 50,
    colour = brewer.pal(9, "Reds"),
    color_title = "red",
    cex = 8,
    digits = 1,
    collapse = TRUE,
    ratio = 2,
    n_collapse = 3,
    n_max = 4,
    hjust_title = 1
)

Session information

#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.2 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] magrittr_2.0.3     RColorBrewer_1.1-3 GimmeMyPlot_0.2.0  rmarkdown_2.29    
#> 
#> loaded via a namespace (and not attached):
#>  [1] sass_0.4.9        generics_0.1.3    tidyr_1.3.1       rstatix_0.7.2    
#>  [5] stringi_1.8.4     digest_0.6.37     evaluate_1.0.3    grid_4.4.2       
#>  [9] fastmap_1.2.0     jsonlite_1.8.9    backports_1.5.0   Formula_1.2-5    
#> [13] purrr_1.0.4       scales_1.3.0      tweenr_2.0.3      jquerylib_0.1.4  
#> [17] abind_1.4-8       cli_3.6.4         rlang_1.1.5       polyclip_1.10-7  
#> [21] munsell_0.5.1     withr_3.0.2       cachem_1.1.0      yaml_2.3.10      
#> [25] tools_4.4.2       ggsignif_0.6.4    dplyr_1.1.4       colorspace_2.1-1 
#> [29] ggplot2_3.5.1     ggpubr_0.6.0      forcats_1.0.0     broom_1.0.7      
#> [33] buildtools_1.0.0  vctrs_0.6.5       R6_2.6.0          lifecycle_1.0.4  
#> [37] stringr_1.5.1     car_3.1-3         MASS_7.3-64       pkgconfig_2.0.3  
#> [41] pillar_1.10.1     bslib_0.9.0       gtable_0.3.6      glue_1.8.0       
#> [45] Rcpp_1.0.14       ggforce_0.4.2     xfun_0.50         tibble_3.2.1     
#> [49] tidyselect_1.2.1  sys_3.4.3         knitr_1.49        farver_2.1.2     
#> [53] htmltools_0.5.8.1 labeling_0.4.3    carData_3.0-5     maketools_1.3.2  
#> [57] compiler_4.4.2