控制字体厚度而不改变字体大小 [英] Control font thickness without changing font size
问题描述
我正在寻找一种方法来控制绘制在 R
中的文本的线条粗细,而不需要改变字符的尺寸。下面是一个例子(不使用 R
):
中间的单词的厚度是顶部的两倍,但尺寸是一样的没有缩放发生)。底部的单词实际上是两个单词:一个红色的单词覆盖在一个沉重的白色单词,以创建分色(特别是有用的注释繁忙的情节)。
下面是一组命令,我试图复制上面的图:
png(font.png,width = 1.02,height = 1.02,units =in,res = 150)
par(ps = 10,font = 1,bg = (),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 = bai)
text(0.5,0.1,FONT,cex = 1,font = 1,col =red)
dev.off()
给出:
par 帮助页面似乎没有针对此的特定设置。任何人有任何想法?
$ b注意改变
大小
在ggplot2
不会产生我想要的效果,最后一次检查。
解决方案尝试添加略微移动的循环模式的文本的多个版本,
library(grid)
stextGrob < - 函数(标号,r = 0.02,x =单位(0.5,npc),y =单位(0.5,npc),
只是=center,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,r ot = 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)
隐形(g)
}
grid.newpage()
grid (gp = gpar(fill =gray))
grid.stext(Yeah,gp = gpar(cex = 4))
在R-help的档案中有一个使用基本图形的版本,从中得到灵感。 $ b
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 usingR
):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:
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
inggplot2
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))
There's a version using base graphics lurking in the archives of R-help, from which this is inspired.
这篇关于控制字体厚度而不改变字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!