【问题标题】:Change font type (e.g., bold) after already specifying font in pdf() function in R在 R 中的 pdf() 函数中指定字体后更改字体类型(例如粗体)
【发布时间】:2019-11-01 07:46:57
【问题描述】:

问题陈述

我正在处理乳胶文档并使用 R 的 pdf() 函数来创建我的绘图。为了匹配我的文档的字体,我在 pdf 函数中指定了字体系列:pdf(family = "LM Roman 12")。但是,在图表(森林图)中,我使用 par(font = ..) 更改为粗体字体以突出显示我的标题。不幸的是,这不再起作用,因为我已经在 pdf() 中指定了字体系列。

示例代码

### Required packages
library(metafor)
library(extrafont)


### My dataset reproduced with: dput()
data.rct.forest <- structure(list(study = c("Fellmeth et al. (2015)", "Gaffney, Ttofi et al. (2019)", 
"Jiménez-Barbero et al. (2016)", "Gaffney, Farrington et al. (2019)", 
"Moy & Hazen (2018)", "Park-Higgerson et al. (2008)"), rct_es = c(1.227, 
1.235, 1.243, 1.333, 0.947, 1.037), rct_es_low = c(0.948, 1.124, 
1.115, 1.087, 0.881, 0.696), rct_es_up = c(1.586, 1.389, 1.361, 
1.669, 1.018, 1.545), quality_score = c(4, 2, 4, 3, 4, 3), group_intervention = c("3_psychosocial: sexual violence", 
"2_psychosocial: bullying", "2_psychosocial: bullying", "2_psychosocial: bullying", 
"1_psychosocial: general violence", "1_psychosocial: general violence"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), .Names = c("study", "rct_es", "rct_es_low", "rct_es_up", 
"quality_score", "group_intervention"))


### Plotting the forest plot
pdf("plot_forest_rct.pdf", height = 5, width = 8, family = "LM Roman 12")

par(font = 1, cex = 1.1)
forest(x = log(data.rct.forest$rct_es), 
       ci.lb = log(data.rct.forest$rct_es_low), 
       ci.ub = log(data.rct.forest$rct_es_up),
       slab = data.rct.forest$study, xlim = c(-4, 2.5), 
       psize = rep(1, 6), atransf = exp,
       refline = 0, alim = c(log(0.5), log(3)), at = c(log(0.5), log(1), log(2), log(3)),
       row = c(0, 3:5, 8:9), ylim = c(0, 13),
       cex = 1, efac = 0.75, xlab = "Odds ratio (log scale)")

### add text for subgroups
par(font = 4, cex = 1.1) # <--- ADDED TEXT SHOULD BE BOLD
text(-4, c(1, 6, 10), pos = 4, c("Psychosocial interventions: sexual violence",
                                 "Psychosocial interventions: bullying",
                                 "Psychosocial interventions: general violence"))

### add text to header
par(font = 4, cex = 1) # <--- ADDED TEXT SHOULD BE BOLD
text(-4, 12, pos = 4, "Author (year)")
text(2.5, 12, pos = 2, "Odds ratio [95% CI]")

dev.off()

这是我得到的:

它应该是这样的(字体系列除外):

我们将不胜感激!

【问题讨论】:

  • text(-4, c(1, 6, 10), pos = 4, c("Psychosocial interventions: sexual violence", + "Psychosocial interventions: bullying", + "Psychosocial interventions: general violence"),font=2, cex=1.1)(完全没有标准)似乎对我有用。

标签: r fonts


【解决方案1】:

我对字体系列有一些问题,但是将fontcex 放在text 调用中似乎可以正常工作:

text(-4, c(1, 6, 10), pos = 4, c("Psychosocial interventions: sexual violence",
                                 "Psychosocial interventions: bullying",
                                 "Psychosocial interventions: general violence"), font=4, cex=1.1)
text(-4, 12, pos = 4, "Author (year)", font=4, cex=1.1)
text(2.5, 12, pos = 2, "Odds ratio [95% CI]", font=4, cex=1.1)

这是使用字体系列“sans”的结果:

【讨论】:

  • cex = .. 工作正常,但font = .. 对我没有任何改变(至少当我使用“LM Roman 12”时。字体是否可能不支持粗体和/或斜体?
  • 尝试将其更改为“font=2”。这也适用于我使用“serif”作为字体系列。
  • 据此 - ftp.math.utah.edu/pub/tex/historic/fonts/latin-modern/lm-1.106/… - LM Roman 12 没有粗体字。但是 10 个可以,所以如果你真的想要两者,你也可以试试。
  • 太棒了!谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2011-08-21
  • 2017-09-13
  • 2014-07-16
  • 2019-06-18
  • 1970-01-01
  • 2017-04-30
  • 2013-05-17
  • 2023-03-11
相关资源
最近更新 更多