Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control font thickness without changing font size

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.

like image 356
Andy Barbour Avatar asked Oct 12 '11 02:10

Andy Barbour


1 Answers

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.

like image 157
baptiste Avatar answered Oct 19 '22 12:10

baptiste