1

I'm trying to label a plot in R with superscript. For example, I have a variable called label:

>label <- colnames(tmp.df);
>label
[1] "ColumnA" "Volume 100mm3", "ColumnC", etc.

I would like to have "3" in the "Volume 100mm3" as superscript in my plot label. I cannot use something like:

label <- c("ColumnA", expression(paste('Volume 100mm'^'3')), "ColumnC");

since the ordering of the column names in tmp.df may change from run to run. So how can I get around this problem?

3 Answers 3

1

You could find the one with the mm by

ind <- grep("mm",label)
splt <- strsplit(label[ind], "mm")[[1]]

and then inject the expression via

label[ind] <- parse(text=sprintf("paste('%smm'^'%s')",splt[1],splt[2]))

If there are multiple strings that indicate the need for expressions, then it should be straightforward to adapt this.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use bquote for this. The * connects "Volume 100" to "mm^3" without a space. If you want a space there, you can use ~ instead of *.

plot(1:10, main = bquote(.("Volume 100") * mm^3))

enter image description here

Comments

0

how about just using the Unicode character for cubed 'SUPERSCRIPT THREE' (U+00B3)? in R, this would be escaped as '100mm\u00B3', or if this is coming from a data file, just use unicode characters directly in the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.