Search code examples
rmathexpressionbar-chartaxis-labels

R: Combine text and mathematical symbols in axis label using barplot


I have a barplot and I want to combine text and mathematical symbol in the label of the y axis. The title is too long so I want to separate it into two lines.

The full text would be something like "A lot of text, more text, mathematical expression". So I used mtext to separate into two different lines.

The first line I could do it without any trouble I use mtext as follows mtext(side = 2, text = "A lot of text", line = 4)

But in the second line, I cannot concatenate the mathematical expression after more text. I tried things like this: mtext(side = 2, text = "more text",expression(DeltaDeltac[T]), line = 3)

and mtext(side = 2, text = "more text"~expression(DeltaDeltac[T]), line = 3)

and of course, didn't work. Somebody asked a similar question, but he didn't have two lines and his mathematical expression was at the beginning of the axis title.

Concatenate strings and expressions in a plot's title

I wonder if there is a way to put the expression between two texts.

Thank you all


Solution

  • As the answer you linked to indicates, the text and math symbol should both go in expression(...), so

    par(mar = c(5, 5, 4, 2))
    barplot(rpois(5, 10))
    mtext(side = 2, text = "A lot of text", line = 4)
    mtext(side = 2, text = expression("more text" ~ Delta[T]), line = 3)
    

    gives you

    enter image description here