Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font type (e.g., bold) after already specifying font in pdf() function in R

Tags:

r

fonts

Problem statement

I am working on a latex document and use R's pdf() function to create my plots. In order to match the font of my document, I specify the font family within the pdf function: pdf(family = "LM Roman 12"). However, within the graph (forest plot) I use par(font = ..) to change to a bold font in order to highlight my headers. Unfortunately, this is not working anymore as I have already specified the font family within pdf().

Example Code

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

This is what I get:

actual state

And this is how it should look like (except for the font family):

target state

Any help will be much appreciated!

like image 382
Matthias Avatar asked Jun 18 '19 15:06

Matthias


People also ask

How do you change the font to bold in R?

Italic, bold, and headers To write text in bold font, use a double asterix or underscores before and after the text.

How do you change the font on a PDF file?

Choose Tools > Edit PDF > Edit . The dotted outlines identify the text and images you can edit. Select the text you want to change. In the right-side Format panel, select a font, font size, or other formatting options.


1 Answers

I have some problem with the font family, but otherwise putting the font and cex inside the text call seems to work fine:

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)

here's the result using the font family "sans":

enter image description here

like image 73
iod Avatar answered Oct 13 '22 23:10

iod