Isha Jamil
Isha Jamil

Reputation: 23

ggplot R, error in grid call and font family

[The font problem is solved but now the problem is with the letters are not on the bar of SBT. It gives me two times SBT, highlighted in figure]

I am using ggplot for a barplot and am getting this error.

In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database​

I tried to install extrafont but still get this error. Im using R.version 3.4.2 Following is my code:

library(ggplot2)
    plot2 <- ggplot(Test, aes(x=factor(Test$habitat3), y=proportion, fill=proportion)) + 
geom_bar(stat="identity", position="stack", colour="black", fill="grey") +
geom_errorbar(aes(ymin=stdlow, ymax=stlup, width=0, position="identity")) +
xlab("Habitat") +
ylab("root abundance") +
coord_cartesian(ylim = c(0, 1)) +
scale_y_continuous(expand=c(0,0)) +
theme(axis.title.x=element_text(margin = unit(c(6, 0, 0, 0), "mm"))) +
theme(axis.title.y=element_text(margin = unit(c(0, 6, 0, 0), "mm"))) +
theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) +
theme(text = element_text(size=14, colour = "black")) +
theme(legend.position="none") +
theme(axis.text.y = element_text(size = 14, colour = "black")) +
theme(axis.text.x = element_text(size = 14, colour = "black")) +
theme(plot.background=element_blank(),  
panel.grid.major=element_blank(),
panel.background=element_blank(),  
panel.grid.minor=element_blank(), 
legend.key=element_blank(),
legend.background=element_blank()) +
theme(text=element_text(family = "Arial")) +
annotate("text", x="PV", y=3.1, label="ab") +
annotate("text", x="WB", y=4.18, label="a") +
annotate("text", x="AF", y=2.98, label="b") +
annotate("text", x="EZ", y=3.43, label="ab")

Upvotes: 1

Views: 6552

Answers (1)

clemens
clemens

Reputation: 6813

You can try to register the fonts first using extrafont.

library(extrafont)

font_import()

loadfonts(device = "postscript")

Then you can use it in ggplot2:

library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))

p + geom_text(family = "Arial")

This produces:

enter image description here

Upvotes: 4

Related Questions