控制字体粗细而不改变字体大小

发布于 2024-12-09 12:08:14 字数 967 浏览 0 评论 0原文

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

variing font Weights

中间单词的粗细为顶部的两倍,但尺寸相同(因此没有发生缩放)。底部的单词实际上是两个单词:一个红色单词覆盖在一个沉重的白色单词上,以创建颜色分离(对于注释繁忙的情节特别有用)。

这是我组合在一起尝试复制上图的一组命令:

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

给出:

replicating in R

所以效果是与将 font-face 更改为粗体相同,但大小差异不足以在重叠时明显注意到。 par 帮助页面似乎没有这方面的具体设置。有人有什么想法吗?

请注意,在我上次检查时,更改 ggplot2 中的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-12-16 12:08:14

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

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

有一个使用隐藏在中的基本图形的版本R-help 的档案,这是受到启发的。

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.

几度春秋 2024-12-16 12:08:14

另一种选择是使用临时 postscript 文件,通过 grImport 转换为形状,

在此处输入图像描述

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

我想可以使用临时光栅图形文件来完成类似的操作,通过某种图像处理算法进行模糊处理并在文本下方显示为光栅。

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.

临风闻羌笛 2024-12-16 12:08:14

你可以尝试:

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

虽然我不确定你会如何做第三个版本的 FONT。

You could try:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文