这是这个问题的延续:Labeling facet strips in ggplot2

通过使用以下代码,我可以获得每个子图顶部的Year=2009,Year=2010:

set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
              quarter=rep(c("Q1","Q2","Q3","Q4"),5),
              sales=40:59+rnorm(20,sd=5))
library(ggplot2)
ggplot(df) +
  geom_line(aes(x=quarter,y=sales,group=year))+
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year=", x)),
    strip.position = "top",
    scales = "free") +
  theme(#panel.spacing = unit(0, "lines"),
    strip.placement = "outside",
    axis.title.x=element_blank(),
    legend.position="none") 

如果我需要一个下标,例如,我需要显示年份ab=2009,而不是年份=2009,我应该如何修改上述代码

我试着给你

... +
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year", bquote(a[b]) ,"=", x)),
    strip.position = "top",
    scales = "free"
  ) +
  ...

但它失败了.

推荐答案

您可以try 使用Unicode值作为下标,例如

library(tidyverse)
set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
              quarter=rep(c("Q1","Q2","Q3","Q4"),5),
              sales=40:59+rnorm(20,sd=5))

ggplot(df) +
  geom_line(aes(x=quarter,y=sales,group=year))+
  facet_wrap(
    .~year,
    labeller = as_labeller(\(x) paste0("Year a\u2090 = ", x)),
    strip.position = "top",
    scales = "free") +
  theme(#panel.spacing = unit(0, "lines"),
    strip.placement = "outside",
    axis.title.x=element_blank(),
    legend.position="none")

创建于2022-11-30年第reprex v2.0.2

这种方法是否有效将取决于您要使用哪些下标字母,即有一个下标"a"(如上图所示),但我不认为有一个下标"b".

R相关问答推荐

从嵌套列表中智能提取线性模型系数

在R中创建一个包含转换和转换之间的时间的列

任意列的欧几里得距离

在某些栏和某些条件下,替换dfs列表中的NA

R根据条件进行累积更改

如何读取CSV的特定列时,给定标题作为向量

仅 Select 超过9行的CSV文件

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

创建列并对大型数据集中的特定条件进行成对比较的更高效程序

为什么这个表格格罗布不打印?

R仅当存在列时才发生变异

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

R将函数参数传递给ggploy

在不重复主题的情况下重新排列组

R中从因数到数字的转换

R中的交叉表

用逗号拆分字符串,并删除一些字符

以列名的字符向量作为参数按行应用自定义函数

移除y轴断开的geom_bar图的外框

如何在调查包中获得与行比例相关的配置项?