1

我有一个图表(下面的简化示例),我想将 x 轴放在顶部。标签使用 element_markdown 来包含换行符。

一切正常,直到我添加 position = "top" 似乎停止应用换行符。你知道为什么吗?

这就是它应该看起来的样子

在此处输入图像描述

并且带有 position = "top" 的代码被注释掉了。

library(tidyverse, ggtext)

periods <-c(1,2,3)
periodLabels <- c("Jan", "Feb<br>21", "Mar")
data <- data.frame(period = periods,
                   y = c(10, 20, 30))
ggplot(data, aes(period, y)) +
  geom_tile() +
  coord_cartesian(expand = FALSE) +
  # scales
  scale_x_continuous(breaks = periods,
                     labels = periodLabels#,
                     #position = "top"
  ) +
  theme_minimal(base_size = 5) +
  theme(
    axis.text.x = element_markdown(size = 8, lineheight = 1.05)
  )
4

1 回答 1

2

您需要在以下位置指定正确的元素(axis.text.x.top现在)theme

periods <-c(1,2,3)
periodLabels <- c("Jan", "Feb<br>21", "Mar")
data <- data.frame(period = periods,
                   y = c(10, 20, 30))
ggplot(data, aes(period, y)) +
    geom_tile() +
    coord_cartesian(expand = FALSE) +
    # scales
    scale_x_continuous(breaks = periods,
                       labels = periodLabels,
                       position = "top"
    ) +
    theme_minimal(base_size = 5) +
    theme(
        axis.text.x.top = element_markdown(size = 8, lineheight = 1.05)
    )

于 2021-05-09T08:57:43.350 回答