ggplot scale_continuous expand argument 如何工作?
How does ggplot scale_continuous expand argument work?
我想弄清楚 scale_continuous()
expand
参数是如何工作的。根据scale_continuous documentation:
A numeric vector of length two giving multiplicative and additive
expansion constants. These constants ensure that the data is placed
some distance away from the axes. The defaults are c(0.05, 0) for
continuous variables, and c(0, 0.6) for discrete variables.
因为它们是 "expansion constants",所以它们不是实际单位。有什么方法可以将它们转换为一些实际测量值以预测实际输出?对于除 0 以外的任何值,我只是尝试随机数,直到它起作用为止。必须有更合适的方法来解决这个问题。
文档写的很清楚。如果手动设置limits
,会更清楚。我将举一些例子来说明它是如何工作的:
第一个参数给出的扩展等于它乘以极限范围;
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7), expand = c(0.5, 0))
# right most position will be 7 + (7-1) * 0.5 = 10
第二个给出了添加到轴两端的绝对扩展:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7), expand = c(0.5, 2))
# right most position will be 7 + (7-1) * 0.5 + 2 = 12
最后,相同的扩展适用于轴的两端。
2019-01-23:我从@C.Liu的回答中了解到,新的expand_scale
函数可以用来实现different扩展下限和上限。 multi
和 add
参数类似于 expand =
所需的两个值,但允许使用长度为 2 的向量来设置下限和上限。有关详细信息,请参阅 C.liu 的回答。
2020-11-25:expand_scale()
至少自版本 3.3.2 起已弃用,请改用 expansion()
。这只是名称更改。 expansion
的参数名称和含义与expand_scale
相同。
expand_scale 可以选择仅微调轴的一端。
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7),
expand = expand_scale(mult = c(0, 0.5),
add = c(2, 0))
# left most position will be 1 - (7-1) * 0.0 -2 = -1,
# right most position will be 7 + (7-1) * 0.5 = 10
This is a convenience function for generating scale expansion vectors for the expand argument of scale__continuous and scale__discrete. The expansions vectors are used to add some space between the data and the axes.
expand_scale(mult = 0, add = 0)
Arguments
mult
vector of multiplicative range expansion factors. If length 1, both the lower and upper limits of the scale are expanded outwards by mult. If length 2, the lower limit is
expanded by mult1 and the upper limit by mult[2].
add
vector of additive range expansion constants. If length 1, both the lower and upper limits of the scale are expanded outwards by add units. If length 2, the lower limit is expanded by add1 and the upper limit by add[2].
我想弄清楚 scale_continuous()
expand
参数是如何工作的。根据scale_continuous documentation:
A numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes. The defaults are c(0.05, 0) for continuous variables, and c(0, 0.6) for discrete variables.
因为它们是 "expansion constants",所以它们不是实际单位。有什么方法可以将它们转换为一些实际测量值以预测实际输出?对于除 0 以外的任何值,我只是尝试随机数,直到它起作用为止。必须有更合适的方法来解决这个问题。
文档写的很清楚。如果手动设置limits
,会更清楚。我将举一些例子来说明它是如何工作的:
第一个参数给出的扩展等于它乘以极限范围;
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7), expand = c(0.5, 0))
# right most position will be 7 + (7-1) * 0.5 = 10
第二个给出了添加到轴两端的绝对扩展:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7), expand = c(0.5, 2))
# right most position will be 7 + (7-1) * 0.5 + 2 = 12
最后,相同的扩展适用于轴的两端。
2019-01-23:我从@C.Liu的回答中了解到,新的expand_scale
函数可以用来实现different扩展下限和上限。 multi
和 add
参数类似于 expand =
所需的两个值,但允许使用长度为 2 的向量来设置下限和上限。有关详细信息,请参阅 C.liu 的回答。
2020-11-25:expand_scale()
至少自版本 3.3.2 起已弃用,请改用 expansion()
。这只是名称更改。 expansion
的参数名称和含义与expand_scale
相同。
expand_scale 可以选择仅微调轴的一端。
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(limits = c(1, 7),
expand = expand_scale(mult = c(0, 0.5),
add = c(2, 0))
# left most position will be 1 - (7-1) * 0.0 -2 = -1,
# right most position will be 7 + (7-1) * 0.5 = 10
This is a convenience function for generating scale expansion vectors for the expand argument of scale__continuous and scale__discrete. The expansions vectors are used to add some space between the data and the axes.
expand_scale(mult = 0, add = 0)
Arguments mult
vector of multiplicative range expansion factors. If length 1, both the lower and upper limits of the scale are expanded outwards by mult. If length 2, the lower limit is expanded by mult1 and the upper limit by mult[2]. add
vector of additive range expansion constants. If length 1, both the lower and upper limits of the scale are expanded outwards by add units. If length 2, the lower limit is expanded by add1 and the upper limit by add[2].