使用添加新标签annotate(geom = "text",
。关闭 x 轴标签的裁剪clip = "off"
in coord_cartesian
.
Use theme
添加额外边距(plot.margin
)并删除(element_blank()
) x 轴文本 (axis.title.x
, axis.text.x
)和垂直网格线(panel.grid.x
).
library(ggplot2)
ggplot(data = df, aes(x = interaction(year, quarter, lex.order = TRUE),
y = sales, group = 1)) +
geom_line(colour = "blue") +
annotate(geom = "text", x = seq_len(nrow(df)), y = 34, label = df$quarter, size = 4) +
annotate(geom = "text", x = 2.5 + 4 * (0:4), y = 32, label = unique(df$year), size = 6) +
coord_cartesian(ylim = c(35, 65), expand = FALSE, clip = "off") +
theme_bw() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
另请参阅@eipi10 的精彩回答:带有嵌套 x 变量的两行轴标签(年份低于月份) https://stackoverflow.com/questions/44616530/axis-labels-on-two-lines-with-nested-x-variables-year-below-months