Home > Mobile >  Error with subscript within list of axis labels
Error with subscript within list of axis labels

Time:04-14

I have a list of axis labels for various water properties. Some have chemical formulae that need subscript. However, I keep getting an error of: non-numeric argument to binary operator.

This is the code for the subscript.

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

This is the Error:

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

CodePudding user response:

Your syntax is wrong here. If you want to use square brackets to indicate subscripts, that can only be done in the context of plotmath expressions:

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

enter image description here

Or, if you want line breaks, you can use unicode escapes to get your subscript numbers:

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

enter image description here

  • Related