0
votes

So I found out that R plotting has a few quirks (I believe): I am plotting barplots and can easily change the font to "times new roman" of these plots using par(family="serif", font=1) However, I also want to plot grouped barplots and the function which is working best for me is the barchart. Unfortunately, the font refuses to change there with the par(family="serif", font=1) command.

Does anyone have an idea how to change the font in barcharts?

In the example below you can alternatively use the barchart-line to plot or the barplot line to see the effect of par(family="serif", font=1) on the font in both cases.

molnames<-c("a")
contactcounts<-c(acounts1,acounts2,acounts3)
d<-data.frame(column1=rep(molnames, each=3),column2=rep(c("1","2","3"), 1), column3=contactcounts)
colour<-c("green", "blue","magenta")
tiff(file="./TEST.tiff", res=1000, width = 8, height = 8,units='in')
par(family="serif", font=1)
barchart(column3 ~ column1,ylab="Y test", groups=column2, d, auto.key = list(columns = 3),  par.settings=list(superpose.polygon=list(col=colour)))
barplot(contactcounts)
dev.off()
1

1 Answers

2
votes

barplot() comes under Base graphics in R and barchart() comes in Lattice graphics. The lattice plots donot use par() settings. They use grid graphics settings.

You can set their values either using trellis.par.set() function in which you provide parameter values as list. The grid parameter values can be set to grid.pars component. A list of valid grid parameters can be see in get.gpar() from package grid, which also has a fontfamily parameter.

trellis.par.set("grid.pars" = list(fontfamily="Monaco") )

if you see warnings/no change in fonts then define the font family yourself by doing:

windowsFonts(Monaco=windowsFont("Monaco"))
trellis.par.set(name="grid.pars", value=list(fontfamily="Monaco"))
barchart(disp~gear)

This works for me