- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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)。
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"
),使用的减淡宽度是要减淡的元素的数据单位的实际宽度。collide
的代码和
dodge
并且,如果需要,提出一个新的、更具体的问题。
x
)和新宽度(
xmin
、
xmax
位置)被计算。元素水平移动的距离刚好足以不与相邻元素重叠。显然,宽元素需要比窄元素移动更多,以避免重叠。
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))
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
.
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
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 = ?)`
?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:
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
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
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)
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)
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,我们需要设置闪避
width
为
geom_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)
关于r - position_dodge 中的宽度参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34889766/
The documentation没有解释这究竟是什么width争论 它指定了谁的宽度? 什么是“单位”? 默认值是多少? 默认值为 width = NULL ,但反复试验表明 width = 0.9
对于两个分类变量的每个交叉分类,我有五个数据点。我试图在误差线之间添加一些均匀的间距,这样它们就不会在多面 ggplot2 图中重叠,但失败了。数据很像这样...... library(ggplot2
我正在尝试使用 ggplot2 函数 geom_text 在条形图上方添加标签(字母)。我的条形图使用 position=position_dodge() 分隔,因此我需要对新标签应用相同的条形图。不
我正在尝试使用 ggplot2 函数 geom_text 在条形图上方添加标签(字母)。我的条形图使用 position=position_dodge() 分隔,因此我需要对新标签应用相同的条形图。不
我的问题与这个 question 密切相关,但是是一个后续问题,而不是重复。我的绘图问题与将 postion_dodge() 与 geom_line() 一起使用有关。 数据: Behaviour R
我正在尝试在 R 中生成一个双因子实验图。它包含三个级别,每个级别都有两个子级别。我想对这些组中的每一个进行注释,但我找不到合理的方法。 这是一个为顶级生成注释的示例: ggplot(mtcars,
问题:我的数据集有一个共享基线(时间点 1)和 2 个重复测量。后续点在 2 个不同的条件下重复(即交叉)。绘图时,结果、误差线和数据点重叠。 library(tidyverse) set.seed(
我在使用 ggplot2 时遇到了以下困难。我正在尝试创建一个带有时间序列数据集的条形图,其中显示每个条形的值和 x 轴下的一个不错的日期格式。问题是: 要设置在柱内或柱上方,必须使用 positio
我看到了 this回答但无法复制。 我得到这样的数据: df = data.frame(x = rep(sample(letters, 4), 2), y = roun
我正在尝试使用 ggplot、geom_poitrange 绘制图形。我有两组,每组有两个点和相应的错误值。我使用的代码如下: group<-c("A","A","B","B") va
我试图在 geom_boxplot 中标记异常值使用 ggrepel::geom_label_repel .当只有一个分组变量时它工作得很好,但是当我尝试将它用于多个分组变量时,我遇到了问题。由于某种
我正在尝试用 geom_text 标记条形图。它在大多数情况下适用于 position_dodge,但在某些情况下,我得到的图看起来像这样。我使用 preserve = 'single' 绕过宽条,但
我想做的是同时使用 position = "fill"和 position = "dodge" geom_bar() 的参数同时不知何故。使用一些样本数据 set.seed(1234) df %
使用下面的代码,刻面 bB 中的标签未正确定位。 问题似乎源于没有position_dodge(preserve="single")为 geom_text (正确的?)。我知道我可以“手动”添加一个空
我想在其中一个闪避条上专门放置一些东西,我该怎么做? 在下面的示例中,我想在条形图的 cat3 组中的 A3 上专门放置一些东西。我知道 cat3 的 x = 3 但由于闪避宽度和条宽的相互依赖性,似
我是一名优秀的程序员,十分优秀!