诊断为什么自定义函数会产生意外警告(大约 position_jitterdodge)

diagnosing why custom function produces unexpected warning (about position_jitterdodge)

我正在复制粘贴我正在使用的自定义函数的简化版本。该功能工作正常,但它产生的 warnings 我一直无法摆脱。这个函数将成为一个包的一部分,因此对于用户来说,不要被这些神秘的错误所困扰是很重要的,因此,我希望以不再产生这些警告的方式更改脚本。

警告是(参见下面完全可重现的示例):

position_jitterdodge requires non-overlapping x intervals

我已经检查过这个关于 position_jitterdodge (Position-dodge warning with ggplot boxplot?) 的问题,但由于该图中没有箱线图,因此没有多大帮助。

对于问题和代码的旷日持久,我们深表歉意。希望提供所有可能的详细信息以帮助诊断此问题。

# loading needed libraries
library(ggplot2)
library(dplyr)
library(devtools)
devtools::install_github("daattali/ggExtra")
library(ggExtra) # attach the development version
library(rlang)

# defining the custom function
ggscatterstats <-
  function(data = NULL,
           x,
           y,
           xfill = "orange",
           yfill = "green",
           marginal = NULL,
           marginaltype = "histogram",
           jitter.width = NULL,
           jitter.height = 0.2,
           dodge.width = 0.75) {
    # preparing a dataframe out of provided inputs
    if (!is.null(data)) {
      # if dataframe is provided
      data <-
        dplyr::select(
          .data = data,
          x = !!rlang::enquo(x),
          y = !!rlang::enquo(y)
        )
    } else {
      # if vectors are provided
      data <-
        base::cbind.data.frame(x = x,
                               y = y)
    }

    # preparing the scatterplotplot
    plot <-
      ggplot2::ggplot(data = data,
                      mapping = aes(x = x,
                                    y = y)) +
      geom_count(
        show.legend = FALSE,
        colour = "black",
        size = 3,
        alpha = 0.5,
        position = position_jitterdodge(
          jitter.width = jitter.width,
          jitter.height = jitter.height,
          dodge.width = dodge.width
        )
      ) +
      geom_smooth(method = "lm",
                  se = TRUE,
                  size = 1.5) +
      theme_grey()

    # marginal plot will be shown by default
    if (is.null(marginal))
      marginal <- TRUE

    if (isTRUE(marginal)) {          
      # creating the ggMarginal plot of a given marginaltype
      plot <-
        ggExtra::ggMarginal(
          p = plot,
          type = marginaltype,
          size = 5,
          xparams = list(fill = xfill,
                               col = "black"),
          yparams = list(fill = yfill,
                               col = "black")
        )
    }

    return(plot)

  }

# using the function
ggscatterstats(data = iris, x = Sepal.Length, y = Petal.Width)
#> Warning: position_jitterdodge requires non-overlapping x intervals
#> Warning: position_jitterdodge requires non-overlapping x intervals

reprex 创建于 2018-02-15 包 (v0.1.1.9000).

一个最小的例子更容易讨论。

这是根据产生此警告的代码改编的最小示例:

library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
    geom_count(size = 3, alpha = 0.5, position = position_jitterdodge())

我不明白你想在这里做什么。我可能是错的,但似乎 tools/concepts 的用法不应一起使用。

geom_count 的全部要点是通过用点的大小表示给定的 x y 坐标对上的观察数量来处理 overplottig。所以你不应该设置大小并且 jittering/dodging 也是不必要的:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
    geom_count() 

另一种可能性是使用 geom_point,您可以将它与抖动相结合,但将抖动与闪避相结合在这里似乎也没有意义:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
    geom_point(size = 3, alpha = 0.5, position = position_jitter()) 

只有当你有一个离散的 x 轴和一个额外的 aestetic 来躲避时,抖动 + 躲避似乎才有意义。在这里,我们创建了一个人为的附加 "sex" 变量,以映射到用于演示的美学颜色。

iris$Sex <- factor(c("M", "F"))
ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sex)) + 
    geom_point(size = 3, alpha = 0.5, position = position_jitterdodge()) 

如果这一切都没有意义,你能解释一下为什么你需要 geom_count 固定大小(更准确地说是 ggplot 术语中的 "set size" )以及抖动和闪避的组合?