gpt4 book ai didi

r - position_dodge 中的宽度参数是什么?

转载 作者:行者123 更新时间:2023-12-01 16:32:38 25 4
gpt4 key购买 nike

The documentation没有解释这究竟是什么width争论

  • 它指定了谁的宽度?
  • 什么是“单位”?
  • 默认值是多少?

  • 默认值为 width = NULL ,但反复试验表明 width = 0.9似乎产生了默认效果(见后记)。然而, I couldn't find where such default value is set in ggplot2 source code .因此,
  • 你能解释一下 ggplot2 中默认闪避是如何实现的吗?代码?

  • 问题的精神是让 ggplot2用户寻找合适的 width没有反复试验的值(value)。
    PS:
    ggplot(data = df) +
    geom_bar(aes(x, y, fill = factor(group)),
    position = position_dodge(), stat = "identity")

    ggplot(data = df) +
    geom_bar(aes(x, y, fill = factor(group)),
    position = position_dodge(0.9), stat = "identity")

    最佳答案

    我将首先对您的三个主要问题作出非常简短的回答。然后我通过几个例子来更彻底地说明答案。

  • 它指定了谁的宽度?geom的宽度要躲避的元素。

  • 什么是“单位”?
    要躲避的元素的数据单位的实际或虚拟宽度。

  • 默认值是多少?
    如果不设置闪避width明确,但依赖于默认值,position_dodge(width = NULL) (或只是 position = "dodge" ),使用的减淡宽度是要减淡的元素的数据单位的实际宽度。


  • 我相信你的第四个问题对 SO 来说太宽泛了。请引用 collide 的代码和 dodge 并且,如果需要,提出一个新的、更具体的问题。

    基于元素的闪避宽度(连同其原始水平位置和堆叠元素的数量)、每个元素的新中心位置( x )和新宽度( xminxmax 位置)被计算。元素水平移动的距离刚好足以不与相邻元素重叠。显然,宽元素需要比窄元素移动更多,以避免重叠。
    为了获得更好的一般躲避感觉和 width的使用特别是论证,我展示了一些例子。我们从一个简单的闪避条形图开始,默认闪避;我们可以使用 position = "dodge"或更明确的 position = position_dodge(width = NULL)
    # some toy data
    df <- data.frame(x = 1,
    y = 1,
    grp = c("A", "B"))

    p <- ggplot(data = df, aes(x = x, y = y, fill = grp)) + theme_minimal()
    p + geom_bar(stat = "identity",
    position = "dodge")
    # which is the same as:
    # position = position_dodge(width = NULL))
    enter image description here
    所以 (1) 谁是 width是在 position_dodge (2) 单位是什么?
    ?position_dodge我们可以读到:

    width: Dodging width, when different to the width of the individual elements


    因此,如果我们使用默认值 width ,即 NULL ,闪避计算基于单个元素的宽度。
    因此,对于您的第一个问题“它指定了谁的宽度?”的简单回答是:单个元素的宽度。
    但是我们当然想知道,“单个元素的宽度”是多少?让我们从酒吧开始。来自 ?geom_bar :

    width: Bar width. By default, set to 90% of the resolution of the data


    一个新问题出现了:什么是分辨率?我们查一下 ?ggplot2::resolution :

    The resolution is is the smallest non-zero distance between adjacent values. If there is only one unique value [like in our example], then the resolution is defined to be one.


    我们试着:
    resolution(df$x)
    # [1] 1
    因此,本例中的默认条宽为 0.9 * 1 = 0.9我们可以通过查看数据 ggplot 来检查这一点。用于使用 ggplot_build 渲染图上的条形图.我们创建一个带有堆叠条形图的绘图对象,条形为默认宽度。
    p2 <- p +
    geom_bar(stat = "identity",
    position = "stack")
    对象中的相关槽是 $data ,这是一个列表,图中的每一层都有一个元素,其顺序与它们在代码中出现的顺序相同。在这个例子中,我们只有一层,即 geom_bar ,让我们看看第一个槽:
    ggplot_build(p2)$data[[1]]

    # fill x y label PANEL group ymin ymax xmin xmax colour size linetype alpha
    # 1 #F8766D 1 1 A 1 1 0 1 0.55 1.45 NA 0.5 1 NA
    # 2 #00BFC4 1 2 B 1 2 1 2 0.55 1.45 NA 0.5 1 NA
    每行包含用于“绘制”单个条形的数据。如您所见,条形的宽度均为 0.9 ( xmax - xmin = 0.9 )。因此,用于计算新躲避位置和宽度的堆叠条的宽度为 0.9 .

    在前面的示例中,我们使用了默认条宽以及默认减淡宽度。现在让我们使条形比上面的默认宽度 (0.9) 稍宽。使用 width参数在 geom_bar将(堆叠的)条形宽度显式设置为例如 1。我们尝试使用与上述相同的减淡宽度( position_dodge(width = 0.9) )。因此,虽然我们已将实际条宽设置为 1,但进行减淡计算时,就好像条宽为 0.9 一样。让我们看看发生了什么:
    p +
    geom_bar(stat = "identity", width = 1, position = position_dodge(width = 0.9), alpha = 0.8)
    p
    enter image description here
    条形重叠,因为 ggplot 水平移动条形,好像它们的(堆叠)宽度为 0.9(设置在 position_dodge 中),而实际上条形的宽度为 1(设置在 geom_bar 中)。
    如果我们使用默认的闪避值,条形会根据设置的条形宽度准确地水平移动:
    p +
    geom_bar(stat = "identity", width = 1, position = "dodge", alpha = 0.8)
    # or: position = position_dodge(width = NULL)

    接下来,我们尝试使用 geom_text 在我们的绘图中添加一些文本。 .我们从默认闪避开始 width (即 position_dodge(width = NULL) ),即闪避基于默认元素大小。
    p <- ggplot(data = df, aes(x = x, y = y, fill = grp, label = grp)) + theme_minimal()
    p2 <- p +
    geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
    geom_text(size = 10, position = position_dodge(width = NULL))
    # or position = "dodge"

    p2
    # Warning message:
    # Width not defined. Set with `position_dodge(width = ?)`
    enter image description here
    躲避文本失败。警告信息呢? “宽度未定义?”。有点神秘。我们需要查阅 ?geom_text 的详细信息部分:

    Note the the "width" and "height" of a text element are 0,so stacking and dodging text will not work by default,[...]Obviously, labels do have height and width, but they are physical units, not data units.


    所以对于 geom_text ,单个元素的宽度为零。这也是您的第二个问题的第一个“官方 ggplot 引用”: width单位以数据单位为单位 .
    让我们看看用于在绘图上呈现文本元素的数据:
    ggplot_build(p3)$data[[2]]
    # fill x y label PANEL group xmin xmax ymax colour size angle hjust vjust alpha family fontface lineheight
    # 1 #F8766D 1 1 A 1 1 1 1 1 black 10 0 0.5 0.5 NA 1 1.2
    # 2 #00BFC4 1 1 B 1 2 1 1 1 black 10 0 0.5 0.5 NA 1 1.2
    确实, xmin == xmax ;因此,以数据为单位的文本元素的宽度为零。
    如何正确躲避宽度为零的文本元素?来自 ?geom_text 中的示例:

    ggplot2 doesn't know you want to give the labels the same virtual width as the bars [...] So tell it:


    因此,为了让 dodge 对 geom_text 使用相同的宽度元素至于 geom_bar当计算新位置时,我们需要将文本元素的“以数据单位为单位的虚拟躲避宽度”设置为与条形相同的宽度。我们使用 width position_dodge 的论据将文本元素的虚拟宽度设置为 0.9(即上例中的条宽):
    p2 <- p +
    geom_bar(stat = "identity", position = position_dodge(width = NULL)) +
    geom_text(position = position_dodge(width = 0.9), size = 10)
    检查用于渲染的数据 geom_text :
    ggplot_build(p2)$data[[2]]
    # fill x y label PANEL group xmin xmax ymax colour size angle hjust vjust alpha family fontface lineheight
    # 1 #F8766D 0.775 1 A 1 1 0.55 1.00 1 black 10 0 0.5 0.5 NA 1 1.2
    # 2 #00BFC4 1.225 1 B 1 2 1.00 1.45 1 black 10 0 0.5 0.5 NA 1 1.2
    现在文本元素的宽度以数据单位表示: xmax - xmin = 0.9 ,即与条形相同的宽度。因此,现在将进行减淡计算,就好像文本元素具有特定宽度,此处为 0.9。渲染图:
    p2
    enter image description here
    文字躲避正确!

    与文本类似,点( geom_point )和误差线(例如 geom_errorbar )的数据单位宽度为零。因此,如果您需要躲避此类元素,则需要指定相关的虚拟宽度,然后基于该虚拟宽度进行闪避计算。见例如 ?geom_errorbar 的示例部分:

    If you want to dodge bars and errorbars, you need to manually specify the dodge width [...] Because the bars and errorbars have different widths we need to specify how wide the objects we are dodging are



    这是一个在连续尺度上具有多个 x 值的示例:
    df <- data.frame(x = rep(c(10, 20, 50), each = 2),
    y = 1,
    grp = c("A", "B"))
    假设我们希望创建一个躲避条形图,每个条形上方都有一些文本。首先,仅使用默认的躲避宽度检查条形图:
    p <- ggplot(data = df, aes(x = x, y = y, fill = grp, label = grp)) + theme_minimal()

    p +
    geom_bar(stat = "identity", position = position_dodge(width = NULL))
    # or position = "dodge"
    它按预期工作。然后,添加文本。我们尝试将文本元素的虚拟宽度设置为与上例中条的宽度相同,即我们“猜测”条的宽度仍然为 0.9,并且我们需要躲避文本元素,好像它们的宽度也为 0.9:
    p +
    geom_bar(stat = "identity", position = "dodge") +
    geom_text(position = position_dodge(width = 0.9), size = 10)
    enter image description here
    显然,条形的躲避计算现在基于与 0.9 不同的宽度,将文本元素的虚拟宽度设置为 0.9 是一个错误的猜测。那么这里的条宽是多少?同样,条宽是“[b]y 默认值,设置为数据分辨率的 90%”。检查分辨率:
    resolution(df$x)
    # [1] 10
    因此,计算新的躲避位置的(默认堆叠)条的宽度现在是 0.9 * 10 = 9 .因此,要躲避条形及其相应的“手拉手”文本,我们还需要将文本元素的虚拟宽度设置为 9:
    p +
    geom_bar(stat = "identity", position = "dodge") +
    geom_text(position = position_dodge(width = 9), size = 10)
    enter image description here

    在我们的最后一个示例中,我们有一个分类 x 轴,只是上面 x 值的“因子版本”。
    df <- data.frame(x = factor(rep(c(10, 20, 50), each = 2)),
    y = 1,
    grp = c("A", "B"))
    在 R 中,因子在内部是一组具有“级别”属性的整数代码。来自 ?resolution :

    If x is an integer vector, then it is assumed to represent a discrete variable, and the resolution is 1.


    现在,我们知道当 resolution为 1,条形的默认宽度为 0.9。因此,在分类 x 轴上, geom_bar 的默认宽度是0.9,我们需要设置闪避 widthgeom_text因此:
    ggplot(data = df, aes(x = x, y = y, fill = grp, label = grp)) +
    theme_minimal() +
    geom_bar(stat = "identity", position = "dodge") +
    # or: position = position_dodge(width = NULL)
    # or: position = position_dodge(width = 0.9)
    geom_text(position = position_dodge(width = 0.9), size = 10)
    enter image description here

    关于r - position_dodge 中的宽度参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34889766/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com