我试图使用
设置:
ggplot2
和scales::trans_new()
来应用自定义缩放我的x轴。但是,当我这样做时,某些轴标签会丢失。有人能帮我弄清楚原因吗?设置:
library(tidyverse)
# the data
ds <- tibble(
myx = c(1, .5, .1, .01, .001, 0),
myy = 1:6
)
# the custom transformation
forth_root_trans_rev <- scales::trans_new(
name = "sign_fourth_root_rev",
transform = function (x) { - abs(x)^(1/4) },
inverse = function (x) { x^4 }
)
情节1:
当我试图绘制这个图时,x = 0
的标签会丢失。
# plot - missing x-label at `0`
ggplot(ds, aes(x = myx, y = myy)) +
geom_line() +
geom_point() +
scale_x_continuous(
trans = forth_root_trans_rev,
breaks = sort(unique(ds$myx)),
)
绘图2
当我在图形两侧添加一些空间时,更多的x轴标签会丢失。
# plot - missing x-labels below 0.5
ggplot(ds, aes(x = myx, y = myy)) +
geom_line() +
geom_point() +
scale_x_continuous(
trans = forth_root_trans_rev,
breaks = sort(unique(ds$myx)),
expand = expand_scale(mult = c(.1, .6))
)
我猜想这与这个旧问题有关:https://github.com/tidyverse/ggplot2/issues/980。然而,我无法弄清如何应用此转换并保留所有x轴标签。
我哪里做错了吗?
forth_root_trans_rev$transform(c(-2, 1, 0, 1, 2))
(# [1] -1.189207 -1.000000 0.000000 -1.000000 -1.189207
)。虽然对于负数x
没有意义 - 但是myx
变量仅包含正值(和零)。 - crlwbmscale_x_continuous(limits=c(1, 10^-6))
对数据进行剪裁,这样在x=0处的点就不会被显示出来了,是吗? - crlwbm