位置多行左调整axis.text在tick下居中

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

我有多行 x 轴标签,我想在标签内向左调整(以便两条线的左侧处于相同的缩进),但位于轴刻度下方的中央。如果我使用 theme(axis.text = element_text(hjust = 0)),文本行会正确缩进到同一级别,但位于刻度线的右侧(请参阅 MWE)。我如何让它们位于刻度线下方的中央?

library(ggplot2)
    library(stringr)

    ## Create a sample data frame
    df <- data.frame(x = 1:5,
                     y = c(2, 4, 3, 5, 6),
                     label = c("Line 1 Label", "Line 2 Label is a bit longer",
                               "Line 3 Label is even longer than the previous one",
                               "Line 4 Label is not so long", "Line 5 Label"))

    ## Wrap the label text with str_wrap()
    df$label <- str_wrap(df$label, width = 15)

    ## Create the plot
    ggplot(df, aes(x, y)) +
        geom_point() +
        scale_x_continuous(breaks = 1:5, labels = df$label) +
        theme(axis.text.x = element_text(hjust = 0))

r ggplot2 label alignment
© www.soinside.com 2019 - 2024. All rights reserved.