[R] How to draw a per mille symbol?

Paul Murrell p.murrell at auckland.ac.nz
Wed Sep 20 21:54:00 CEST 2006


Hi


Gavin Simpson wrote:
> On Wed, 2006-09-20 at 15:28 +1200, Paul Murrell wrote:
>> Hi
>>
>>
>> Gavin Simpson wrote:
>>> Dear list,
>>>
>>> Following advice posted to this list a while back by Prof Ripley [1], I
>>> have been trying to draw a per mille character [2] in an axis label.
>>>
>>> This should give the correct character:
>>>
>>> plot(1:10, ylab = "\u2030")
>>>
>>> but all I get is '"S'. I'm running linux (FC5) and have fonts installed
>>> that have the correct character (viewed in the Gnome character map at
>>> least).
> 
> Thanks for your reply Paul, and also to Andrew Robinson for his earlier
> reply. I had initially avoided trying to use the encoding argument and
> pdf() as I had planned to include the code to produce the graphics in a
> Sweave document and AFAICS there is no way to pass extra arguments to
> the code generating the pdf figures in Sweave? 


In cases like this, I put an explicit pdf() call (and dev.off()) in my
Sweave code chunk and then explicitly \includegraphics{} the resulting
figure.


> Of course, my original
> plan was ignorant of the details of font encodings and mappings to
> single byte encodings in pdf and postscript devices and wouldn't have
> worked anyway.
> 
>>
>> I get the same thing (and using xfd I see the per mille character in the 
>> font I'm using).  I'm afraid I'm not sure why this is happening;  I can 
>> get a number of other "unusual" characters to work (e.g., \u20ac), but 
>> there appear to be some characters that do not draw correctly.  I used 
>> the following code to explore the default Helvetica font I've got and I 
>> can't see a rational pattern in the misbehaviour.
>>
>> x11(width=5, height=5)
>> grid.prompt(TRUE)
>> digits <- c(0:9, letters[1:6])
>> for (i in c("00", "01", "02", "03", "1e", "20", "21", "22")) {
>>      grid.newpage()
>>      for (j in 1:16) {
>>          for (k in 1:16) {
>>              pushViewport(viewport(x=j/16, y=1-k/16,
>>                                    width=1/16, height=1/16,
>>                                    just=c("right", "bottom")))
>>              eval(parse(text=paste('grid.text("\\u',
>>                           i, digits[k], digits[j], '")', sep="")))

                grid.text(paste("\\u", i, digits[k], digits[j], sep=""),
                          y=1, just="top",
                          gp=gpar(col="grey", cex=0.5))

>>              popViewport()
>>          }
>>      }
>> }
>>
> 
> That is a nice tool for looking at the font glyphs, which I can see
> being very useful in working out which unicode number matches the
> character you want to display. I'm not too clued up on grid graphics
> yet, would it be easy to modify the above to print out the \uXXXX code
> above each glyph?


Sure.  See code above.

Paul


>>> I have also tried plotting to a pdf device with a font family that the
>>> character map tool shows I have a per mille glyph for, e.g.:
>>>
>>> pdf("~/tmp/test_per_mille.pdf", paper = "a4", family = "URWBookman")
>>> plot(1:10, ylab = "\u2030")
>>> dev.off()
>>>
>>> But all I get here is a period or a dot-like symbol.
>>
>> This is an encoding problem I think.  For producing PDF output, the 
>> character string gets converted to a single-byte encoding.   If your 
>> default locale is ISOLatin1 like mine then you won't see the per mille 
>> because that character (called perthousand in the Adobe afm's) is not in 
>> the ISOLatin1 encoding.  If you explicitly use an encoding that does 
>> include perthousand (like WinAnsi) then the conversion to single-byte 
>> encoding works.  For example, this works (for me at least) ...
>>
>> pdf("WinAnsi_per_mille.pdf", encoding="WinAnsi")
>> plot(1:10, ylab = "\u2030")
>> dev.off()
>>
>> Paul
> 
> Thanks for this, which works fine for me also.
> 
> All the best,
> 
> G
> 
>>
>>> I've tried this in R 2.4.0 alpha [4] and R 2.5.0 to be [4] as my
>>> self-compiled R 2.3.1-patched dies when plotting Unicode characters
>>> (fixed in 2.4.0 alpha and above [3])
>>>
>>> Can anyone point me in the right direction to get this working?
>>>
>>> TIA,
>>>
>>> G
>>>
>>> [1] http://finzi.psych.upenn.edu/R/Rhelp02a/archive/48709.html
>>> [2] like a "%" but with 2 circles at the bottom not one, see
>>> http://en.wikipedia.org/wiki/Permille
>>> [3] see thread at http://article.gmane.org/gmane.comp.lang.r.devel/9704
>>> [4] R version 2.4.0 alpha (2006-09-19 r39410)
>>> [5] R version 2.5.0 Under development (unstable) (2006-09-19 r39410)

-- 
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