【问题标题】:diagnosing why custom function produces unexpected warning (about position_jitterdodge)诊断自定义函数产生意外警告的原因(关于 position_jitterdodge)
【发布时间】:2018-02-16 02:44:10
【问题描述】:

我正在复制粘贴我正在使用的自定义函数的简化版本。该函数工作正常,但它产生的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 package (v0.1.1.9000) 于 2018 年 2 月 15 日创建。

【问题讨论】:

    标签: r ggplot2 dplyr plyr rlang


    【解决方案1】:

    一个小例子更容易讨论。

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

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

    我不明白你想在这里做什么。我可能错了,但似乎使用了不应该一起使用的工具/概念。

    geom_count 的重点是通过用点的大小表示给定 x y 坐标对上的观察次数来处理 overplottig。所以你不应该设置大小,抖动/闪避也是不必要的:

    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 轴和一个额外的审美来躲避时才有意义。在这里,我们创建了一个人为的附加“性别”变量,以映射到演示的颜色。

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

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

    【讨论】:

    • 警告发生在ggplot2:::collide,其中定义了间隔(在您的最后一张图中,这些间隔是红点和相应绿点之间的空间)。我们可以通过运行ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sex)) + geom_point(size = 3, alpha = 0.5, position = position_jitterdodge(dodge.width = 3)) 重现 OP 的警告
    • OP 可以通过运行 ggscatterstats(data = iris, x = Sepal.Length, y = Petal.Width,dodge.width = 0.1) 来删除这些警告,但作为您的答案细节,最好首先确保当前方法有意义。
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 2014-10-03
    • 2018-02-03
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2021-05-15
    相关资源
    最近更新 更多