mohammad gerami
mohammad gerami

Reputation: 153

Changing boxplot Fonts in R

I am trying to change boxplot font to "Times New Roman" through this command in R:

library(extrafont)
loadfonts(device="win")
fonts()
boxplot(sp ~ grp, data = mydata,family="Times New Roman")

But the fonts in the plot does not change. How to I change all the text in boxplot graph? Thanks in advance You can see the results here

Upvotes: 2

Views: 9993

Answers (2)

bastistician
bastistician

Reputation: 147

The font family cannot be changed by setting family in boxplot(). Instead, use par(family = "Times New Roman") to modify this graphical parameter before calling boxplot().

A reproducible example follows:

boxplot(count ~ spray, data = InsectSprays)
boxplot(count ~ spray, data = InsectSprays, family = "serif")
# -> 'family' has no effect here

par(family = "serif")
boxplot(count ~ spray, data = InsectSprays)
# -> font has changed

Upvotes: 3

Sandipan Dey
Sandipan Dey

Reputation: 23129

Try this:

windowsFonts(
  A=windowsFont("Arial Black"),
  B=windowsFont("Bookman Old Style"),
  C=windowsFont("Comic Sans MS"),
  D=windowsFont("Times New Roman")
)
par(mfrow=c(2,2))
for (f in LETTERS[1:4]) {
  par(family=f)
  boxplot(Sepal.Length ~ Species, data = iris, main = "Title", font=2)  
}

enter image description here

Upvotes: 3

Related Questions