italics and normal text in a main plot title
Asked Answered
G

3

19

I am plotting a graph in R but the italics function and it is being pretty frustrating under the main title:

Mapped territories of different C. austriacus individuals at the Far Gardens coral reef site

Any help would be appreciated.

Goncourt answered 31/5, 2014 at 11:51 Comment(0)
G
25

You didn't give any information about your data but if the problem is italics in the title, maybe this code could help:

plot(rnorm(100), main = substitute(paste(italic('p value'), " = 0.01")))

enter image description here

See also this question.

Grijalva answered 31/5, 2014 at 12:7 Comment(2)
Technically plot() isn't a lattice functions. Also, plot(rnorm(100),main=expression(italic('p value') == 0.01 )) would work just as well without the paste() since you're not really doing substitution. Though the OP will need paste to do plot(rnorm(100),main=expression(paste("Mapped territories of different ", italic("C. austriacus"), " individuals at the Far Gardens coral reef site")))Abbess
@MrFlick. You do not need quotes if you use proper plotmath separators.Michelinemichell
M
19

Personally I think using paste to construct plotmath expression is "ugly"; This is an alternative the more clearly demonstrates "clean" use of expression:

 plot(rnorm(100),main=expression( italic(p~value) == 0.01 ))

The other reason to use expression is that it will be accepted by Lattice functions whereas the substitute approach will not:

xyplot(1~1,main=substitute( paste(italic('p value'), " = 0.01" )))
#Error in paste(italic("p value"), " = 0.01") : 
#  could not find function "italic"

It would succeed if expression() were used inside the substitute call but it's excess baggage in that instance. I complained to Deepayan Sarkar once and his response was that substitute returns an unevaluated "call" rather than a true 'expression'.

Michelinemichell answered 31/5, 2014 at 22:58 Comment(3)
Can I use expression and replace 0.01 with dynamically generated value?Meri
You you can use bquote to do that at least with ordinary plot. Do a search on [r] bquote title for worked examples. There can be difficulties with lattice, so you might need to wrap as.expression around the result of bquote because the value it returns is technically not an R 'expression'Michelinemichell
for variables other option in: #57456327Droop
D
6

I think other users have answered this, but I found it not so cut and dry, and built off of their input because it gets tricky for long titles like the one in your example.

Here is the cleanest line of code I could conjure for combined italic/normal texts, using a generic plot... let me know how it works for your data (or anyone who reads this and finds it doesn't work with certain graphs, inbox me, I enjoy learning and would rather share than just store it in my noggin)

plot(1:10, main = expression('Mapped territories of different '*italic(C.~austriacus)*' individuals at the Far Gardens coral reef site'))

Now, to line break with expression or like terms that allow italicized or superscript/subscript text (other than simple labels), you can use the atop function to evenly break apart super long labels. One may prefer to use the preview app for final editing and labels, but if you want to stick to R for everything, you can use the following example:

plot(1:10, main = expression(atop('Mapped territories of different '*italic(C.~austriacus),  ' individuals at the Far Gardens coral reef site')))

which gives: plot with long mixed title

Thanks to @42-

Downhill answered 1/1, 2018 at 2:6 Comment(5)
Cool stuff. Would this atop method also work with e.g. ylab?Kraut
actually i tried it without atop, just expression to insert italicized subscripts to ylab and it worked :)Kraut
cool, did it work with mixed text? (mixed as in italic text in between regular text?). If you have the line of code, I would enjoy to see it and try it myself.Downhill
yes it was mixed italic with normal text and even italic subscript.. i just put this into my boxplot function: ylab=expression('Log of ('*italic(d[N])*'+0.01) / ('*italic(d[S])*'+0.01)')Kraut
I was going to put here a little demonstration but somehow the italics there doesn't work (but you can download the notebook, it works localy). I tweeted the Rnotebook author about this, maybe he'll know what's wrong there..Kraut

© 2022 - 2024 — McMap. All rights reserved.