Search code examples
rggplot2labelplotmath

How to make a hyphen, not minus sign, when using expression() to label in R?


I'm using expression() in my plot's x-axis label to create a square-root symbol over my measure's name to indicate that the data has been transformed using square-root transformation. However, my measure's name ("CES-D") has a hyphen in it. When I write it in the expression(), the hyphen becomes a minus or en dash character with space around it.

qplot(1:10, 1:10) + 
    labs(x = expression(sqrt(CES-D~scores)), 
         y = "CES-D scores")

Hyphens different in x and y axis labels

Notice the hyphens are different in the x and y axis labels. In the x-axis label, it looks like square root of "CES minus D scores".

How do I make a regular hyphen character inside an expression() for text?


Solution

  • Try backticks around CES-D:

    qplot(1:10, 1:10) + labs(x = expression(sqrt(`CES-D`~scores)), y = "CES-D scores")