Search code examples
rplotaxis-labels

R Create multiple graphs with different title and axis names with subscripts and superscript


I've spent hours searching and trying to find a way to use expression or bquote to come up with a way to create a new title for each graph I make. The problem I have is that the title name changes for each graph and there is a different number of subscripts and superscripts in each name. How do I format my "main=" statement to change to the new title name with the various subscripts and superscripts that I want?

# This is the format I want to show on the graphs
# Title Graph1 = "Total pH"  (no super or subscripts)
# Title Graph2 = "Total NO3-"  where "3" subscript and "-" superscript
# Title Graph3 = "Total H2PO3-" where "2" subscript, "3" subscript, and "-" superscript
# Title Graph4 = "Total K+" where "+" is superscript

n <- c("pH", "NO3-", "H2PO3-", "K+")

for (j in n) {
    dev.new(width=10, height = 10)
    plot (x=2,y=3, main=j)
}

Solution

  • One way is to use the expression with the plotmath notation to create a vector of desired expressions and then sequence through them.

    n<-expression(pH, NO['3']^'-', H[2]~PO[3]^'-', K^'+')
    for (j in n) {
      dev.new(width=10, height = 10)
      plot (x=2,y=3, main=j )
    }
    

    enter image description here

    See ?plotmath for the syntax of the notation.