Skip to content

Plot math symbol with string and number

12 messages · Rasmus Liland, John Smith, David Stevens +3 more

#
Dear Helpers,

I would like to make plots with titles for different data sets and
different parameters. So a useful title should combine data name
and parameter for clarity. The following is a simplified code example with
two plots. The first title doesn't show sigma as a math symbol, while the
second one doesn't contain the s value as a numeric value - I could
manually change the s value, but when there are many different s values,
this is not a good solution. Thanks!

s <- 1
y <- rnorm(100)
plot(y, main=paste("data", "sigma=", s))
plot(y, main=expression(paste("data", sigma,"=", s)))
#
?plotmath
On Sun, Aug 16, 2020, 14:53 John Smith <jswhct at gmail.com> wrote:

            

  
  
#
Specifically, see the "how to combine "math" and numeric variables" in the
Examples therein.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sun, Aug 16, 2020 at 3:18 PM Bert Gunter <bgunter.4567 at gmail.com> wrote:

            

  
  
#
On Sun, Aug 16, 2020 at 3:18 PM Bert wrote:
| On Sun, Aug 16, 2020, 14:53 John wrote:
| | 
| | I would like to make plots with 
| | titles for different data sets and 
| | different parameters. The first 
| | title doesn't show sigma as a math 
| | symbol, while the second one 
| | doesn't contain the s value as a 
| | numeric value
| |
| | s <- 1
| | y <- rnorm(100)
| | plot(y, main=paste("data", "sigma=", s))
| | plot(y, main=expression(paste("data", sigma,"=", s)))
|
| ?plotmath

Dear John, read ?plotmath, it is good, I 
was not aware of its existence; then 
backquote s like so:

	plot(y, main=bquote(paste(
	  "data" ~~ widehat(aleph) 
	  %notin% .(s)^.(s))))

V

r
#
Thanks to Dunkan, Rasmus and Bert. Will keep the very useful tips. Best!
On Sun, Aug 16, 2020 at 9:13 PM Rasmus Liland <jral at posteo.no> wrote:

            

  
  
#
John - one more approach

plot(y,main=parse(text=paste0('data ~~ sigma == ',s)))

Good luck

David Stevens
On 8/17/2020 8:23 AM, John Smith wrote:
#
Thanks David for a quite interesting suggestion: Indeed I didn't know
paste0! Best!

On Mon, Aug 17, 2020 at 12:26 PM David K Stevens <david.stevens at usu.edu>
wrote:

  
  
#
On 2020-08-17 03:13, Rasmus Liland wrote:
Plotmath seems to be the right way to do it.  But without reading 
plotmath I'd have gone with this:

plot(y, main=paste("data", "\u03C3=", s))
#
On 2020-08-17 22:14 +0100, cpolwart at chemo.org.uk wrote:
| On 2020-08-17 03:13, Rasmus Liland wrote:
| | On Sun, Aug 16, 2020 at 3:18 PM Bert wrote:
| | |
| | | ?plotmath
| | 
| | Dear John, read ?plotmath, it is 
| | good, I was not aware of its 
| | existence; then backquote s like 
| | so:
| 
| Plotmath seems to be the right way to 
| do it.  But without reading plotmath 
| I'd have gone with this:
| 
| plot(y, main=paste("data", "\u03C3=", s))

Good; for me copying the sigma unicode 
character into that line works too:

	plot(y, main=paste("data?=", s))

How curious 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200817/96989127/attachment.sig>
#
"Plotmath seems to be the right way to do it. "

Not sure I agree with that. Paul Murrell put together plotmath around 2000
prior to the widescale development and adoption of the unicode standard
(corrections/modifications welcome!).  So at the time, there really was no
other way to handle this for most OS'es. With UTF8 now being generally
supported for Unicode, plotmath constructions may not be needed for simple
symbol labeling, as here. Of course for more complex symbolism (fractions,
integrals, ...) it will be. ?plotmath talks about this and has links to
further issues and options, btw.

In other words, unicode may indeed be better than my suggestion of plotmath
here.

I would welcome comments from others who are more knowledgeable about this
than I am.

Bert
On Mon, Aug 17, 2020 at 2:14 PM <cpolwart at chemo.org.uk> wrote:

            

  
  
#
I think that comment is fair *on graphics devices that can handle unicode*.

So that is true for Cairo-based graphics devices, but not for the pdf() 
or postscript() devices, for example.

Paul
On 18/08/20 9:54 am, Bert Gunter wrote:

  
    
#
On 2020-08-18 11:02 +1200, Paul Murrell wrote:
| On 18/08/20 9:54 am, Bert Gunter wrote:
| | On Mon, Aug 17, 2020 at 2:14 PM <cpolwart at chemo.org.uk> wrote:
| | | 
| | | Plotmath seems to be the right way 
| | | to do it.  But without reading 
| | | plotmath I'd have gone with this:
| | | 
| | | plot(y, main=paste("data", "\u03C3=", s))
| | 
| | "Plotmath seems to be the right way 
| | to do it."
| | 
| | Not sure I agree with that. Paul 
| | Murrell put together plotmath around 
| | 2000 prior to the widescale 
| | development and adoption of the 
| | unicode standard 
| | (corrections/modifications 
| | welcome!).  So at the time, there 
| | really was no other way to handle 
| | this for most OS'es. With UTF8 now 
| | being generally supported for 
| | Unicode, plotmath constructions may 
| | not be needed for simple symbol 
| | labeling, as here. Of course for 
| | more complex symbolism (fractions, 
| | integrals, ...) it will be. 
| | ?plotmath talks about this and has 
| | links to further issues and options, 
| | btw.
| | 
| | In other words, unicode may indeed 
| | be better than my suggestion of 
| | plotmath here.
| 
| I think that comment is fair *on 
| graphics devices that can handle 
| unicode*.
| 
| So that is true for Cairo-based 
| graphics devices, but not for the 
| pdf() or postscript() devices, for 
| example.

Eventhough I'm a heavy user of pdf(), I 
didn't notice at first that the sigma 
gets converted to two dots with the 
error messages

	Warning messages:
	1: In title(...) :
	  conversion failure on 'data ?= 1' in 'mbcsToSbcs': dot substituted for <cf>
	2: In title(...) :
	  conversion failure on 'data ?= 1' in 'mbcsToSbcs': dot substituted for <83>
	3: In title(...) :
	  conversion failure on 'data ?= 1' in 'mbcsToSbcs': dot substituted for <cf>
	4: In title(...) :
	  conversion failure on 'data ?= 1' in 'mbcsToSbcs': dot substituted for <83>

like if it was a Han character (most 
likely three dots in that case), I just 
ran with cpolwart's suggestion in 
whatever is the default on the R 
console, probably that Cairo thing.  
png() works ofc. 

V

r

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200818/fc34fe32/attachment.sig>