ggplot2 rainplot函数图的问题

5lwkijsr  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(145)

我创建了这个函数来表示使用一个随机图。它嵌入在我正在构建的R包中:

#' 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)

如果我使用数据库传递适当的参数,我会得到下图:

我有一些问题:
1.如何修改分布图和箱线图之间的间距?
1.如何根据类(变量1,变量2,变量3)为每个分布给予颜色

q3qa4bjr

q3qa4bjr1#

修复了重叠箱线图和密度的问题,在ggdist::stat_halfeye中设置scale参数。默认情况下,此值设置为.9,但考虑到justification和箱形图宽度scale=.7应该可以工作。请参见下面的示例。要为箱线图、点和/或密度着色,您必须在colorfill aes上Map:

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()`).

相关问题