我有一个图表(下面是简化的例子),我想把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)
)发布于 2021-05-09 16:57:43
您需要在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)
)https://stackoverflow.com/questions/67455877
复制相似问题