In R STUDIO, how to apply the same font type and size in ggplot?

First, let’s generate a simple data.

Genotype= c("A","B","C","D","E")
Yield= c(70, 75, 71, 88, 90)
se= c(2,3,2,3,4)
DataA= data.frame(Genotype,Yield,se)
print(DataA)
  Genotype Yield se
1        A    70  2
2        B    75  3
3        C    71  2
4        D    88  3
5        E    90  4

Then I’ll make a bar graph using ggplot2.

if(!require(ggplot2)) install.packages("ggplot2")
library(ggplot2)

Fig1= ggplot(data=DataA, aes(x=Genotype, y=Yield, fill=Genotype))+
  scale_fill_manual(values=c("azure4","darkolivegreen4",
  "cadetblue", "Dark red","Blue")) +
  geom_bar(stat="identity", position="dodge", width=0.7, size=1) +
  geom_errorbar(aes(ymin= Yield-se, ymax=Yield + se),
  position=position_dodge(0.7), width=0.2, color='Black') +
  scale_y_continuous(breaks=seq(0,150,50), limits=c(0,150)) +
  labs(x="Genotype", y="Yield (g/m2)") +
  theme(axis.title.x= element_text (family="serif", size=15, 
        color="black"),
        axis.title.y= element_text (family="serif", size=15, 
        color="black"),
        axis.text.x= element_text(family="serif", size=15),
        axis.text.y= element_text(family="serif", size=15),
        legend.position= 'none',
        axis.line= element_line(size=0.5, colour="black")) 

options(repr.plot.width=5.5, repr.plot.height=5)
print(Fig1)

ggsave("Fig1.png", plot= Fig1, width=5.5, height= 5, dpi= 300)

Now, I made a bar graph like above, but in the code to make this bar graph, I repeated font type and size over and over to set up the same font type and size in both graph title and text (also in x and y axis).

theme (
axis.title.x= element_text (family="serif", size=15, color="black"), 
axis.title.y= element_text (family="serif", size=15, color="black"), 
axis.text.x= element_text(family="serif", size=15), 
axis.text.y= element_text(family="serif", size=15), 
legend.position= 'none', 
axis.line= element_line(size=0.5, color="black")
)

I want to reduce this repeated codes, and the solution is using theme_grey().

axis.title.x=element_text(family="serif",size=15,color="black"),
axis.title.y=element_text(family="serif",size=15,color="black"),
axis.text.x=element_text(family="serif",size=15),
axis.text.y=element_text(family="serif",size=15),

these codes can be replaced by theme_grey(base_size=15, base_family="serif")

Then, this is a new code. The length of code was reduced, and it seems a more simple code.

if(!require(ggplot2)) install.packages("ggplot2")
library(ggplot2)

Fig2= ggplot(data=DataA, aes(x=Genotype, y=Yield, fill=Genotype))+
  scale_fill_manual(values=c("azure4","darkolivegreen4","cadetblue",
  "Dark red","Blue")) +
  geom_bar(stat="identity", position="dodge", width=0.7, size=1) +
  geom_errorbar(aes(ymin= Yield-se, ymax=Yield + se),
  position=position_dodge(0.7), width=0.2, color='Black') +
  scale_y_continuous(breaks=seq(0,150,50), limits=c(0,150)) +
  labs(x="Genotype", y="Yield (g/m2)") +
  theme_grey(base_size=15, base_family="serif")+
  theme(legend.position= 'none',
        axis.line= element_line(size=0.5, colour="black"))

options(repr.plot.width=5.5, repr.plot.height=5)
print(Fig2)

ggsave("Fig2.png", plot= Fig2, width=5.5, height= 5, dpi= 300)

and you can obtain the same bar graph.

theme_grey()is only applied the format of panel, which means font type and size of legend is independent from theme_grey(). So we need to set it up separately.


We aim to develop open-source code for agronomy ([email protected])

© 2022 – 2025 https://agronomy4future.com – All Rights Reserved.

Last Updated: 12/02/2021