Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters and superscripts on plot axis titles

I am trying to make a y-axis title with both a special character and a superscript. I am able to do this, but I want the closing parentheses not to be superscripted. And that's what I'm having a problem with. I think its just a placing of my parenthesis, but I've tried (seemingly) everything.

plot(WatexCl, ConcuM, col = as.numeric(1), pch = as.numeric(Depth),     xlab = expression(paste("Concentration Cl  ( ", mu, "moles/g dry wt)")),     ylab = expression(paste("Average Conc of S- on plates ( ", mu, "Moles/cm"^"2"),)),     data = plates) 
like image 283
Valerie S Avatar asked Apr 01 '13 01:04

Valerie S


People also ask

How do I superscript axis labels in R?

The expression() command used to make superscripts and subscripts in axis labels. Note that R does not “like” subscripts beginning with numbers and continuing with letters! So [2xyz] gives an error but [2 * xyz] is fine.

How do you write subscript and superscript in R?

Subscripts and SuperscriptsTo indicate a subscript, use the underscore _ character. To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets.

How do you use YLAB and XLAB in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How do you bold an AXE in R?

Make Axis Title Text Bold Font with element_text() To make both x and y-axis's title text in bold font, we will use axis. title argument to theme() function with element_text(face=”bold”). Note now both x and y axis's title text are in bold font.

How to use superscripts in R axis labels?

Use superscripts in R axis labels 3 Strings & Expressions in Plot Titles, Labels etc 3 R: Generate plot axis break labels with superscripts programmatically 1 Aligning awkward plot and axis titles 1 Superscript, subscript, and greek symbol all in one axis title in ggplot2 2

Can I add a superscript to the title of a plot?

As you can see, the main title of the plot contains a superscript. Note that you may add a superscript to any other kind of text (e.g. axis labels or text within a graph) in a Base R graphic.

How to display a superscript in the title using TeX?

Display a superscript in the title using the ^ character. The ^ character modifies the character immediately following it. Include multiple characters in the superscript by enclosing them in curly braces {}. Include the Greek letters and in the text using the TeX markups \alpha and \mu, respectively. Add text at the data point where t = 300.

How do you add a subscript to a plot in R?

Figure 2: R Plot with Subscript in Main Title. As you can see in Figure 2, our plot title contains a subscript. It is also possible to add several superscripts and subscripts to a text element using the expression function and the symbols ^ and [] as shown in the previous examples.


1 Answers

The one thing that often users fail to grasp is that you invariably don't need to quote strings and paste them together when used in an expression for a plot label. It is usually simpler to use the layout tools directly (e.g. ~ and *). For example:

df <- data.frame(y = rnorm(100), x = rnorm(100))  plot(y ~ x, data = df,      ylab = expression(Average ~ Conc ~ of ~ S- ~ on ~ plates ~                         (mu ~ Moles ~ cm^{-2} ~ dry ~ wt)),      xlab = expression(Concentration ~ Cl ~ (mu ~ moles ~ g^{-1} ~ dry ~ wt))) 

Alternatively, you can include strings for longer sections of text; in this case it is arguably easier to do:

plot(y ~ x, data = df,      ylab = expression("Average Conc of S- on plates" ~                          (mu ~ moles ~ cm^{-2} ~ "dry wt")),      xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt"))) 

but note there is no need to paste strings and other features here.

Both produce:

enter image description here

Note the issue plotmath has with the superscript 2. You may wish to add some extra space for the y-axis margin to accommodate that:

op <- par(mar = c(5,4.5,4,2) + 0.1) plot(y ~ x, data = df,      ylab = expression("Average Conc of S- on plates" ~                           (mu ~ moles ~ cm^{-2} ~ "dry wt")),      xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt"))) par(op) 

producing

enter image description here

like image 138
Gavin Simpson Avatar answered Oct 12 '22 00:10

Gavin Simpson