在不改变字体大小的情况下控制字体粗细 [英] Control font thickness without changing font size

查看:36
本文介绍了在不改变字体大小的情况下控制字体粗细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来控制在 R 中绘制的文本的线条粗细,而不会改变字符的尺寸.这是一个示例(不使用 R):

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

中间字的厚度是顶部的两倍,但尺寸相同(因此没有发生缩放).最下面的字实际上是两个字:一个红色的字叠加在一个沉重的白色字上,以创建分色(对于注释繁忙的情节特别有用).

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

给予:

所以效果和把 font-face 改成粗体是一样的,只是大小差异不大,叠加的时候不明显.par 帮助页面似乎没有针对此的特定设置.有人有什么想法吗?

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?

注意在 ggplot2 中更改 size 也不会产生我想要的效果,上次我检查过.

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

推荐答案

您可以尝试添加文本的多个版本,以圆形图案略微移动,

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

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

在 R-help 的档案中有一个使用基本图形的版本,它的灵感来自于此.

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

这篇关于在不改变字体大小的情况下控制字体粗细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆