[R] Plotting sigma symbol with unicode and turning into pdf

Paul Murrell p.murrell at auckland.ac.nz
Fri Aug 14 03:51:22 CEST 2009


Hi


Jonathan R. Blaufuss wrote:
> Paul, You solution worked out really well when I ran my code in R.
> However, when I try to turn the plot into a pdf, the unicode string
> no longer seems to function and instead of the sigma symbol there are
> just two periods (See example code below).
> 
> The following is the code working in the R environment just like I
> want it to look:
> 
> set.seed(1) 
 > Data=rnorm(100,sd=10000)
 > plot(density(Data))
> text(25000,0.00004, 
 > paste("\u03c3 = ",
 >       format(round(sd(Data),digits=3),big.mark=",")),
 >       font=2, col="blue")
> 
> 
> 
> Now when I try to turn the plot into a pdf, the sigma symbol no
> longer appears. It is replaced by two periods.
> 
> pdf(file="C:/Rquestion.pdf")  
 > ### Note: You can choose a place for this file to be saved
 > set.seed(1)
 > Data=rnorm(100,sd=10000)
> plot(density(Data)) 
 > text(25000,0.00004,
 > paste("\u03c3 = ",
>       format(round(sd(Data),digits=3),big.mark=",")), 
 >       font=2, col="blue")
> dev.off()
> 
> 
> Is it possible to make the unicode string function when writing to a
> pdf or do I need to form the sigma symbol some other way?


Here are a few suggestions (roughly in increasing order of complexity) ...

1.
The standard symbol font for PDF is actually quite "heavy", so you could 
try just doing the original plotmath approach.  Even though the sigma 
symbol is not bolded, it stands up reasonably well next to the bold 
digits (in PDF).

pdf(file="RquestionDefault.pdf")
set.seed(1)
Data=rnorm(100,sd=10000)
plot(density(Data))
text(25000,0.00003,
      bquote(bold(sigma==.(mySigma)),
             list('mySigma'=format(round(sd(Data),digits=3),
                                   big.mark=","))),
      col='blue')
dev.off()

2.
The problem that you are seeing with the two dots in PDF is due to the 
fact that the (multibyte) Unicode sequence "\u03c3" has to be converted 
to a single byte "encoding" to be stored in the PDF file. 
Unfortunately, the default encoding (ISOLatin1) does not contain the 
'sigma' character, so the conversion fails (hence the dots).  You can 
specify an encoding that does contain the 'sigma' character, e.g., 
CP1253, but you also need to use a *font* that contains the 'sigma' 
character.  Here's an example of this solution that just uses the 
standard fonts that R knows about ...

pdfFonts(stdsym=Type1Font("standardsymbol",
            c("Helvetica.afm",
              "s050000l.afm",
              "Helvetica-Oblique.afm",
              "Helvetica-BoldOblique.afm",
              "Symbol.afm"),
            encoding="CP1253"))

pdf("RquestionEncSym.pdf")
set.seed(1)
Data=rnorm(100,sd=10000)
plot(density(Data))
text(25000,0.00004,
	paste("\u03c3 = ",
               format(round(sd(Data),digits=3),big.mark=",")),
	family="stdsym", font=2, col="blue")
dev.off()

What this is doing is specifying a new font "family" that has the 
standard symbol font in the place where you would normally have the bold 
font.  Note that the encoding for this font is CP1253.  This new font 
family is then specified in the call to text(..., family="stdsym").

There are two unsatisfactory aspects to this solution:  the sigma symbol 
is still not bold (it is in fact just the same symbol as for solution 
1), PLUS the digits are now not bold either.  So this is not really a 
very good solution, but it demonstrates some of the ideas we need to get 
a better solution.

3.
The problem with the previous solution is that it used the standard 
symbol font for the entire piece of text that was being drawn (the sigma 
symbol and the digits).  If we go back to using plotmath expressions, we 
can improve on this because plotmath allows us to use different fonts 
for different parts of a single piece of text.  Here's a solution that 
demonstrates this approach ...

pdfFonts(stdsym=Type1Font("standardsymbol",
            c("Helvetica.afm",
              "Helvetica-Bold.afm",
              "s050000l.afm",
              "Helvetica-BoldOblique.afm",
              "Symbol.afm"),
            encoding="CP1253"))

pdf("RquestionEncSym2.pdf")
set.seed(1)
Data=rnorm(100,sd=10000)
plot(density(Data))
text(25000,0.00003,
      bquote(bold(italic("\u03c3") == .(mySigma)),
             list('mySigma'=format(round(sd(Data),digits=3),
                                   big.mark=","))),
      family="stdsym", col="blue")
dev.off()

The new font family is slightly different in this example (the standard 
symbol font is now in place of the italic font within the font family) 
and the major difference is the use of plotmath to draw the text.  In 
particular, the sigma symbol is being drawn with an italic font face, 
italic("\u03c3"), while the rest of the expression is bold.  This means 
that the digits remain bold while the sigma symbol comes from the 
standard symbol font.

Only problem is, the symbol character is STILL not bold.  But this 
example has demonstrated the ideas we need to propose a final possible 
solution.

4.
The extra step that we need, beyond the previous solution, is to make 
use of a font that has a BOLD sigma symbol in it.  The following code 
gives an example, but this is based on fonts that are available on my 
particular Linux box as part of the LaTeX installation;  you may have to 
change the specification of the new font to point to a font that you 
have (which may require you to download/install a new font).

pdfFonts(BS=Type1Font("BoldSymbol",
            c("Helvetica.afm",
              "Helvetica-Bold.afm",
              "/usr/share/texmf/fonts/afm/public/pl/plmib10.afm",
              "Helvetica-BoldOblique.afm",
              "Symbol.afm"),
            encoding="CP1253"))

pdf(file="Rquestion.pdf")
set.seed(1)
Data=rnorm(100,sd=10000)
plot(density(Data))
text(25000,0.00003,
      bquote(bold(italic("\u03c3") == .(mySigma)),
             list('mySigma'=format(round(sd(Data),digits=3),
                                   big.mark=","))),
      family="BS", col="blue")
dev.off()
embedFonts("Rquestion.pdf",
            fontpaths="/usr/share/texmf/fonts/type1/public/pl/")

The other difference in this example is the extra call to embedFonts() 
at the end.  This is necessary when using a font that your PDF viewer is 
unlikely to know about.  This step makes the special font part of the 
PDF file, so the result should be able to be viewed anywhere.

Now, the quality of the final result will depend on which font you use 
and how strong its bold symbols are.  For me, the result is comparable 
to what you get from Solution 1, so its questionable whether the extra 
effort is worthwhile.  But mucking around with this example found a bug 
in R that I was able to fix, so all's not lost :)

Hope there is something in there that you can work with.

Paul


> Thanks for the help,
> 
> Jonathan
> 
> 
> 
> 
> ----- Original Message ----- From: "Paul Murrell"
> <p.murrell at auckland.ac.nz> To: "Jonathan R. Blaufuss"
> <blaufusj at carleton.edu> Cc: "Scott Sherrill-Mix"
> <shescott at mail.med.upenn.edu>, r-help at r-project.org Sent: Wednesday,
> August 12, 2009 4:40:49 PM GMT -06:00 US/Canada Central Subject: Re:
> [R] Using bold font with bquote
> 
> Hi
> 
> Jonathan R. Blaufuss wrote:
>> Scott, Your suggestion works great for changing the numbers to bold
>>  font, but is it possible to change the sigma symbol and the equals
>>  sign to bold font as well? I've poked around ?plotmath and am I
>> right in saying that there is a different method for controlling
>> symbol fonts?
> 
> 
> R graphics only recognizes a plain symbol font (it has this weird
> idea that a symbol font is a font face like bold or italic).
> 
> For your particular example, because it is not a complex math
> formula, you might be able to do a workaround by constructing a
> simple string and specifying the symbol that you want using Unicode.
> Depending on what system and fonts you have, the following might work
> ...
> 
> text(25000,0.00004, paste("\u03c2 = ", 
> format(round(sd(Data),digits=3),big.mark=",")), font=2, col="blue")
> 
> 
> Paul
> 
> 
>> Thank you for your help,
>> 
>> Jonathan
>> 
>> ----- Original Message ----- From: "Scott Sherrill-Mix" 
>> <shescott at mail.med.upenn.edu> To: "Jonathan R. Blaufuss" 
>> <blaufusj at carleton.edu> Cc: r-help at r-project.org Sent: Wednesday, 
>> August 12, 2009 12:43:12 PM GMT -06:00 US/Canada Central Subject:
>> Re: [R] Using bold font with bquote
>> 
>>> From ?plotmath, it looks like when using expressions you set the 
>>> font
>> inside the expression (e.g. bold(x)). It looks you tried this
>> already but I wonder if there was something tiny out of place since
>> the following works for me:
>> 
>> text(25000,0.00003,bquote(bold(sigma==.(mySigma)),list('mySigma'=format(round(sd(Data),digits=3),big.mark=","))),
>>  col='blue')
>> 
>> Scott
>> 
>> Scott Sherrill-Mix Department of Microbiology University of 
>> Pennsylvania 402B Johnson Pavilion 3610 Hamilton Walk Philadelphia,
>>  PA  19104-6076
>> 
>> 
>> 
>> On Wed, Aug 12, 2009 at 12:36 PM, Jonathan R. 
>> Blaufuss<blaufusj at carleton.edu> wrote:
>>> I'm trying to annotate a density plot and I'm using bquote to
>>> paste the sigma symbol next to the numeric text of the standard
>>> deviation calculation that I am performing. I have been able to
>>> successfully turn the sigma symbol and numeric output the color
>>> blue, but when I try to change the font of the text to bold, R
>>> doesn't seem to recognize the "font=" command in the same way
>>> here as it does with "col=". (My code is below)
>>> 
>>> set.seed(1) Data=rnorm(100,sd=10000) plot(density(Data)) 
>>> text(25000,0.00003, bquote(sigma==.(mySigma), 
>>> list('mySigma'=format(round(sd(Data),digits=3),big.mark=","))), 
>>> col="blue")
>>> 
>>> After searching the help files I've tried using the expression 
>>> command with "bold()" as well as inserting "font=2" after the
>>> color command. However, I can't seem to get it to work.
>>> 
>>> Can someone please point me to a resource that will help me
>>> figure this out?
>>> 
>>> Thank You,
>>> 
>>> Jonathan
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the
>>> posting guide http://www.R-project.org/posting-guide.html and
>>> provide commented, minimal, self-contained, reproducible code.
>>> 
>> ______________________________________________ R-help at r-project.org
>>  mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE
>> do read the posting guide
>> http://www.R-project.org/posting-guide.html and provide commented,
>> minimal, self-contained, reproducible code.
> 

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list