轴标签列表中的下标错误

Error with subscript within list of axis labels

我有各种水特性的轴标签列表。有些具有需要下标的化学式。但是,我不断收到以下错误:二元运算符的非数字参数。

这是下标的代码。

axisLabels <- list('DA' = c('Dissolved Ammonium (NH'[4]*') \n(mg/l)', 'Sampling Site'),
               'DNI' = c('Dissolved Nitrite \n(mg NO'[2]*'/L)', 'Sampling Site'),
               'DNA' = c('Dissolved Nitrate \n(NO'[3]*')', 'Sampling Site'),
               'DS' = c('Dissolved Sulphate \n(mg SO'[4]*'/L)', 'Sampling Site'))

这是错误:

Error in "Dissolved Ammonium (NH"[4] * ") \n(mg/l)" : 
non-numeric argument to binary operator

这里你的语法有误。如果你想用方括号表示下标,那只能在plotmath表达式的上下文中完成:

axisLabels <- list('DA' = expression(Dissolved~Ammonium~(NH[4])~mg/l),
                   'DNI' = expression(Dissolved~Nitrite~(mg~NO[2]/L)),
                   'DNA' = expression(Dissolved~Nitrate~(NO[3])),
                   'DS' = expression(Dissolved~Sulphate~(mg~SO[4]/L)))

par(mfrow = c(2, 2))
lapply(axisLabels, function(x) plot(1:10, rnorm(10), xlab = x))

或者,如果你想要换行符,你可以使用 unicode 转义来获取你的下标数字:

axisLabels <- list('DA' = c('Dissolved Ammonium (NH\u2084) \n(mg/l)'),
               'DNI' = c('Dissolved Nitrite \n(mg NO\u2082/L)'),
               'DNA' = c('Dissolved Nitrate \n(NO\u2083)'),
               'DS' = c('Dissolved Sulphate \n(mg SO\u2084/L)'))

par(mfrow = c(2, 2))
lapply(axisLabels, function(x) plot(1:10, rnorm(10), xlab = x))