8

I'm looking for a way to control the line thickness of text plotted in R without having the dimensions of the characters change. Here's an example (not using R):

varying font weights

The middle word has a thickness of twice the top, yet the dimensions are the same (so no scaling happened). The bottom word is actually two words: a red word overlain on a heavy white word, to create color separation (especially useful for annotating a busy plot).

Here's a set of commands I threw together to try and replicate the figure above:

png("font.png",width=1.02, height=1.02, units="in", res=150)
par(ps=10, font=1, bg="light gray", col="black", mai=rep(0.02,4), pin=c(1,1))
plot.new()
box()
text(0.5,0.85,"FONT",cex=1)
text(0.5,0.6,"FONT",cex=2)
text(0.5,0.3,"FONT",cex=2,col="white")
text(0.5,0.3,"FONT",cex=1,col="red")
text(0.5,0.1,"FONT",cex=1, font=2, col="white")
text(0.5,0.1,"FONT",cex=1, font=1, col="red")
dev.off()

giving:

replicating in R

So the effect is the same as changing the font-face to bold, but the size difference is not big enough to be noticeable when overlain. The par help page doesn't appear to have a specific setting for this. Anyone have any ideas?

Note changing size in ggplot2 doesn't produce the effect I want either, last time I checked.

Andy Barbour
  • 8,783
  • 1
  • 24
  • 35

3 Answers3

11

You could try adding multiple versions of the text slightly shifted in a circular pattern,

yeah


library(grid)
 stextGrob <- function (label, r=0.02, x = unit(0.5, "npc"), y = unit(0.5, "npc"), 
                        just = "centre", hjust = NULL, vjust = NULL, rot = 0, check.overlap = FALSE, 
                        default.units = "npc", name = NULL, gp = gpar(), vp = NULL){

   let <- textGrob("a", gp=gp, vp=vp)
   wlet <- grobWidth(let)
   hlet <- grobHeight(let)

   tg <- textGrob(label=label, x=x, y=y, gp=gpar(col="red"),
                  just = just, hjust = hjust, vjust = vjust, rot = rot,
                  check.overlap = check.overlap, 
                  default.units = default.units)

   tgl <- c(lapply(seq(0, 2*pi, length=36), function(theta){

     textGrob(label=label,x=x+cos(theta)*r*wlet,
              y=y+sin(theta)*r*hlet, gp=gpar(col="white"),
              just = just, hjust = hjust, vjust = vjust, rot = rot,
              check.overlap = check.overlap, 
              default.units = default.units)

     }), list(tg))


   g <- gTree(children=do.call(gList, tgl), vp=vp, name=name, gp=gp)

 }

 grid.stext <- function(...){
   g <- stextGrob(...)
   grid.draw(g)
   invisible(g)
 }

 grid.newpage()
 grid.rect(gp=gpar(fill="grey"))
 grid.stext("Yeah", gp=gpar(cex=4))

There's a version using base graphics lurking in the archives of R-help, from which this is inspired.

baptiste
  • 75,767
  • 19
  • 198
  • 294
3

Another option using a temporary postscript file, converted to a shape by grImport,

enter image description here

library(grImport)

cat("%!PS 
     /Times-Roman findfont 
     100 scalefont 
     setfont 
     newpath 
     0 0 moveto 
     (hello) show", file="hello.ps")

PostScriptTrace("hello.ps", "hello.xml")
hello <- readPicture("hello.xml")
grid.rect(gp=gpar(fill="grey"))
grid.picture(hello,use.gc = FALSE, gp=gpar(fill="red", lwd=8, col="white"))

I imagine something similar could be done with a temporary raster graphic file, blurred by some image processing algorithm and displayed as raster below the text.

baptiste
  • 75,767
  • 19
  • 198
  • 294
0

You could try:

text(...,"FONT", vfont = c('serif','bold'))

Although I'm not sure how you'd do the third version of FONT.

joran
  • 169,992
  • 32
  • 429
  • 468