【发布时间】:2021-03-23 09:27:54
【问题描述】:
我在ggplot2 的辅助x 轴上显示一个标签,它按预期工作。
library(ggplot2)
p <- ggplot(mtcars, aes(wt)) +
geom_histogram() +
scale_x_continuous(sec.axis = sec_axis(trans = ~.,
labels = "median",
breaks = 3.2))
# label present
p
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
但如果我更新主要的x-axis,次要的x-axis 将被删除。
# label absent
p + scale_x_continuous(limits = c(1, 6), breaks = c(seq(1, 6, 1)))
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 2 rows containing missing values (geom_bar).
由reprex package (v1.0.0) 于 2021-03-23 创建
我原以为它会更新,而不是删除(因为辅助轴是主轴的变换)。
是否有可能更新主轴,同时保留次轴信息?
附言
这是在包函数的上下文中,该函数默认在辅助轴上显示标签。然后,用户可以根据需要使用ggplot2 函数更新绘图。这意味着用户有时可以更改轴,我希望该功能仍然显示标签。我将更新我的问题以提供此上下文。
【问题讨论】: