R ggplot2 中的垂直居中轴文本

问题描述 投票:0回答:1

我正在使用

ggplot2
ggridges
绘制多个区域。我想将 y 轴文本垂直居中。有没有办法以自动化的方式实现这一点?作为手动方法,我目前通过
theme(axis.text.y = element_text(vjust = -2.5))
指定偏移量。然而,理想情况下,我想简单地将其集中到不同物种的相应区域,而不管地块的大小。如果有另一种方法可以在没有
ggridges
的情况下实现类似的情节,这也可以。

library(ggplot2)
library(ggridges)

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(scale = 1) +
  theme(axis.text.y = element_text(vjust = -2.5))
r ggplot2 ggridges
1个回答
0
投票

一种技巧可能是使用分面,将标签移到一侧,然后删除填充,使它们位于数据的中间。

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(scale = 1) +
  facet_grid(vars(Species), scales = "free_y", switch = "y") +
  scale_y_discrete(expand = expansion(add = c(0,0))) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        strip.background = element_blank(),
        strip.text.y.left = element_text(angle = 0))

© www.soinside.com 2019 - 2024. All rights reserved.