Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues with a ggplot2 rainplot funtion figure

I created this function to represent using a raning plot. It is embedded in a R package I’m building:

#' Exploratory Data Analysis: Rainingplot
#'
#' This function creates a rainingplot to represent continuous data. It uses a classification variable
#' @param db D.B_Final databse obtained in the incorporation - data step.
#' @param var1 Dependent variable. It must be a continuous variable
#' @param var2 Classification variable
#' @return A rainingplot classified by the `var2` argument.
#' @export
#'
rainplot_fig<-function(db,var1,var2){
  fig<-ggplot2::ggplot(db, ggplot2::aes({{var1}}, {{var2}})) +
    ggdist::stat_halfeye(adjust = .5, width = .7, .width = 0, justification = -.2, point_colour = NA) +
    ggplot2::geom_boxplot(width = .2, outlier.shape = NA) +
    ggplot2::geom_jitter(height =0.05,size=.9,width = 1, alpha = .3) +
    ggplot2::theme_minimal()
  return(fig)

And if I pass the proper parameters using a database I get the following figure:
enter image description here

I have some issues:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. How can I modify the space between the distribution figure and the boxplot?
  2. How can I give colors to each distribution according to the classes (Variante 1, Variante 2, Variante 3)

>Solution :

The fix the issue with the overlapping boxplot and densities set the scale parameter in ggdist::stat_halfeye. By default this is set to .9, but to take account of the justification and the boxplot width scale=.7 should work. See the examples below. To color the boxplots, points and/or densities you have to map on the color or fill aes:

rainplot_fig <- function(db, var1, var2) {
  ggplot2::ggplot(db, ggplot2::aes({{ var1 }}, {{ var2 }})) +
    ggdist::stat_halfeye(
      ggplot2::aes(fill = {{ var2 }}),
      adjust = .5, width = .7, .width = 0, justification = -.2,
      point_colour = NA, scale = .7
    ) +
    ggplot2::geom_boxplot(ggplot2::aes(color = {{ var2 }}),
      width = .2, outlier.shape = NA
    ) +
    ggplot2::geom_jitter(ggplot2::aes(color = {{ var2 }}),
      height = 0.05, size = .9, width = 1, alpha = .3
    ) +
    ggplot2::theme_minimal()
}


rainplot_fig(iris, Sepal.Length, Species)


library(palmerpenguins)

rainplot_fig(penguins, bill_length_mm, species)
#> Warning: Removed 2 rows containing missing values (`stat_slabinterval()`).
#> Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
#> Warning: Removed 2 rows containing missing values (`geom_point()`).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading