How to customize the title format in facet_wrap()?

How to customize the title format in facet_wrap()?


Graph Partitioning Using facet_wrap() in R Studio


By following my previous post, you can understand how to obtain the figure below.

library (ggplot2)
library (dplyr)
library(tidyr)

Nitrogen= c(rep("N0", 5), rep("N1", 5))
Cultivar_1= c(50,49,48,47,46,60,62,63,64,62)
Cultivar_2= c(55,57,56,55,54,65,66,67,64,63)
Cultivar_3= c(60,62,63,65,59,60,59,57,56,58)
dataA= data.frame(Nitrogen, Cultivar_1, Cultivar_2, Cultivar_3)

df= data.frame(
               dataA %>%
               pivot_longer(
               cols=c(Cultivar_1, Cultivar_2, Cultivar_3),
               names_to="Genotype", values_to="Yield")
               )

dataB= data.frame(df %>%
        group_by(Genotype, Nitrogen) %>%
        dplyr::summarize(across(c(Yield), 
                          .fns = list(Mean = mean, 
                                      SD = sd, 
                                      n = length,
                                      se = ~ sd(.)/sqrt(length(.))))))

ggplot (data=dataB, aes(x=Genotype, y=Yield_Mean, fill=Nitrogen)) +
  geom_bar(stat="identity",position="dodge") +
  geom_errorbar(aes(ymin= Yield_Mean-Yield_se, ymax= Yield_Mean+Yield_se),
                position=position_dodge(0.9), width=0.2) +
  scale_fill_manual(values=c("Gray","Dark green")) +
  scale_y_continuous(breaks=seq(0, 100, 20), limits=c(0, 100))+
  facet_wrap(~Nitrogen) +
  labs(x="Cultivar", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position='bottom',
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        axis.line=element_line(linewidth=0.5, colour="black"))

If you copy and paste the code above into your R console, you can obtain the same figure as shown above.


Now, I’d like to change the title format by removing the title border.

ggplot (data=dataB, aes(x=Genotype, y=Yield_Mean, fill=Nitrogen)) +
  geom_bar(stat="identity",position="dodge") +
  geom_errorbar(aes(ymin= Yield_Mean-Yield_se, ymax= Yield_Mean+Yield_se),
                position=position_dodge(0.9), width=0.2) +
  scale_fill_manual(values= c("Gray","Dark green")) +
  scale_y_continuous(breaks= seq(0, 100, 20), limits= c(0, 100))+
  facet_wrap(~Nitrogen) +
  labs(x="Cultivar", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position='bottom',
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        axis.line=element_line(linewidth=0.5, colour="black"),
        strip.background=element_rect(color="white", 
                                      linewidth=0.5, linetype="solid"))

Next, I’d like to draw a line in the title. Please refer to the code below.

ggplot (data=dataB, aes(x=Genotype, y=Yield_Mean, fill=Nitrogen)) +
  geom_bar(stat="identity",position="dodge") +
  geom_errorbar(aes(ymin= Yield_Mean-Yield_se, ymax= Yield_Mean+Yield_se),
                position=position_dodge(0.9), width=0.2) +
  scale_fill_manual(values= c("Gray","Dark green")) +
  scale_y_continuous(breaks= seq(0, 100, 20), limits= c(0, 100))+
  facet_wrap(~Nitrogen) +
  annotate("segment", x=1, xend=3, y=Inf, yend=Inf, color="black", lwd=1)+
  labs(x="Cultivar", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position='bottom',
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        axis.line=element_line(linewidth=0.5, colour="black"),
        strip.background=element_rect(color="white", 
                                      linewidth=0.5, linetype="solid"))
full code: https://github.com/agronomy4future/r_code/blob/main/How_to_customize_the_title_format_in_facet_wrap().ipynb


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.