将第二个 x 轴添加到 ggplot

Add second x-axis to ggplot

我建了这个地块:

df_ebf <- df_ebf %>%
  map_df(rev)

labels.minor <- c("nie","selten","manchmal", "mehrmals", "oft", "sehr oft", "immerzu")

ggplot(data=df_ebf, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
  geom_line(color = "#003560") +
  geom_point(color = "#003560") +
  coord_flip() +
  labs(x="EBF-Skalen") +
  scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), sec.axis = dup_axis(),expand = c(0,0)) +
  scale_x_discrete(expand = c(0,0)) +
  theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank())

现在我想在下面的 x 轴下方添加第二个 x 轴 labels.minor。

我在论坛中寻找解决方案时发现 and this,但它对我的情况不起作用。

如果您的 labels.minor 位于中断的确切位置,您只需将标签与换行符粘贴在一起即可。示例如下:

library(ggplot2)

df <- data.frame(
  x = runif(10, min = 0, max = 6),
  y = letters[1:10]
)

labels.minor <- c("nie","selten","manchmal", "mehrmals", "oft", "sehr oft", "immerzu")

ggplot(df, aes(x, y)) +
  geom_path(aes(group = -1)) +
  geom_point() +
  scale_x_continuous(limits = c(0, 6), breaks = 0:6,
                     labels = paste0(0:6, "\n", labels.minor),
                     sec.axis = sec_axis(~.x, breaks = 0:6))

reprex package (v1.0.0)

于 2021-04-10 创建