How to use italics, bold, and new lines in a plot title in R

Viewed 592

I am using barplot in R and need the title to follow APA format. I need to write 'Figure 1' in bold, then include an empty line, then write the title in italics, and it all needs to be left aligned. So, the title needs to read like this:

Figure 1

Reasons for Stops Compared

The best solution I have come up with so far is using the title() and expression() functions. However, when I do this, the second line (title) becomes indented and no matter what I try I cannot get the right combination of formatting to work without introducing a new problem. See example

here is example code to use

par(mar = c(5,4,5,1))

barplot(c(1, 1, 1), beside = T, col = 'black',
        names.arg = c("Investigatory", "Safety", "Checkpoint"),
        ylab = "Proportions", xlab = "Black / White")

title(main = expression(bold("Figure 1\n\n")~italic("Reasons for Stops Compared")), line = 1, adj = 0)

abline(h = c(1))
1 Answers

Use mtext. In the question the image has the second line indented but the text of the question does not. I have added spaces in mtext to indent but remove them if you don't want that. Also have added code to revert par at the end. c(1) equals 1 so get rid of the c.

opar <- par(mar = c(5,4,5,1))

barplot(c(1, 1, 1), beside = T, col = 'black',
        names.arg = c("Investigatory", "Safety", "Checkpoint"),
        ylab = "Proportions", xlab = "Black / White")

title(main = ~ bold("Figure 1"),  line = 3, adj = 0)
mtext(~ italic("        Reasons for Stops Compared"), adj = 0)

abline(h = 1)
par(opar)

screenshot

Related