【问题标题】:ggtext : element_markdown not working with position = "top"ggtext:element_markdown 不适用于 position = "top"
【发布时间】:2021-07-30 23:56:21
【问题描述】:

我有一个图表(下面的简化示例),我想将 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)
  )

【问题讨论】:

    标签: r ggplot2 ggtext


    【解决方案1】:

    您需要在theme 中指定正确的元素(现在axis.text.x.top):

    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)
        )
    
    

    【讨论】:

    • 谢谢亚历克斯,这对我有用。如果有人知道为什么,那就太好了。 ggtext 会被关闭吗?
    • 似乎是一个已知问题:您需要更改主题中的元素 (github.com/wilkelab/ggtext/issues/5)。我编辑了答案
    猜你喜欢
    • 2014-07-27
    • 2011-05-11
    • 2012-07-26
    • 1970-01-01
    • 2018-01-17
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多