现在ggh4x已被弃用,ggplot2中应如何实现坐标轴截断?
我对UMAP的坐标轴格式有一种非常明确的偏好,这是我的个人喜好,也是与我们实验室的做法保持一致。
以前我使用下面的代码,坐标轴就会是这样的(我想要的效果!)
# Define small axes
axis <- ggh4x::guide_axis_truncated(
trunc_lower = unit(0, "npc"),
trunc_upper = unit(2, "cm"),
arrow = grid::arrow(type = "closed", length = unit(0.2, "cm"))
)
# Remove ticks
riz_theme <- theme(axis.line = element_line(arrow = arrow()),
axis.title = element_text(hjust = 0), axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
# Plot - here I implement my defined small axes
DimPlot(COs, group.by = "Phase") + NoLegend() + guides(x = axis, y = axis) + riz_theme + riz_legend_pdf + riz_guides_pdf + ggtitle('By Phase')
我知道我可以设置NoAxes(),再复制并粘贴一个截图,但上述做法效果很好,如果只是一个简单的修复,重新使用起来也会很方便。
解决方案
现在你可以使用 ggplot2::guide_axis(cap="upper") 在上限处截断坐标轴(并且 ggh4x::guide_axis_truncated 会就此发出一个警告)。不过,我看不到有使用 unit 来设置轴线长度的选项。相反,你可以使用 scale_x/y_xxx 来设置上限断点:
library(ggplot2)
axis <- ggh4x::guide_axis_truncated()
#> Warning: `guide_axis_truncated()` was deprecated in ggh4x 0.3.0.
#> ℹ Please use `ggplot2::guide_axis(cap = TRUE)` instead.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
theme_classic()
axis <- guide_axis(
cap = "upper",
theme = theme(
axis.line = element_line(
arrow = grid::arrow(type = "closed", length = unit(0.2, "cm"))
),
axis.text = element_blank(),
axis.ticks = element_blank()
)
)
p +
scale_x_continuous(breaks = 75) +
scale_y_continuous(breaks = 15) +
guides(
x = axis,
y = axis
)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。
