Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings & Expressions in Plot Titles, Labels etc

Tags:

r

plotmath

I am trying to paste together user a user provided string and a fixed string in a plot title.

A simple case that works, of course:

userTitle <- "user title" # Example 1
fullTitle <- paste(userTitle, ": Results", sep = "")
plot(1:10, main = fullTitle)

But what if the user's title contains an expression? Here's some things I've tried:

# This works, but doesn't include the fixed string # Example 2
userTitle <- expression(italic(Genus)~italic(species)) # EDIT: this was missing
fullTitle <- bquote(.(userTitle))
plot(1:10, main = fullTitle)

Try to add the fixed string. Some things that don't quite work:

fullTitle <- bquote(.(userTitle)~':'~Results) # Example 3
plot(1:10, main = fullTitle) # title missing .(userTitle)

fullTitle <- bquote(paste("Results:", .(userTitle))) # Example 4
plot(1:10, main = fullTitle) # title missing .(userTitle)

But this example, from here works just fine [EDIT: link was to wrong question].

x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))

My Example 4 looks pretty much exactly this last one, but doesn't behave the same. There are so many combos of bquote, expression, substitute and I've looked at a lot of answers, but I am likely missing something really small. Any advice on how to get the user string and fixed string together if the user string contains an expression in this case? Thanks.

like image 493
Bryan Hanson Avatar asked Mar 13 '26 19:03

Bryan Hanson


1 Answers

I can do it with a formula:

userTitle <- italic(Genus)~italic(species) 
plot(1, 1., main=substitute( userTitle*": Results" , 
                              list(userTitle=userTitle) ) )

And now with an expression:

userTitle <- expression( italic(Genus)~italic(species) )
plot( 1, 1, main= bquote(.(eval(userTitle))*":"~Results) )
like image 152
IRTFM Avatar answered Mar 16 '26 09:03

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!