How to draw a y-axis border when using facet_wrap() in R? (feat. scales=”free”)

How to draw a y-axis border when using facet_wrap() in R? (feat. scales=”free”)


df= tibble::tibble(
  location= rep(c("A", "B"), each= 16L),
  cultivar= c(
    "CV_2", "CV_2", "CV_1", "CV_1", "CV_2", "CV_2", "CV_1", "CV_1", "CV_2",
    "CV_2", "CV_1", "CV_1", "CV_2", "CV_2", "CV_1", "CV_1", "CV_1", "CV_2",
    "CV_1", "CV_2", "CV_2", "CV_1", "CV_1", "CV_2", "CV_2", "CV_2", "CV_1",
    "CV_1", "CV_2", "CV_2", "CV_1", "CV_1"),
  irrigation= rep(rep(c("Irrigation_No", "Irrigation_Yes"), 2), each= 8L),
  fertilizer= rep(rep(c("N1", "N0"), 4), each= 4L),
  rep= c(2, 3, 1, 4, 2, 3, 1, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2,
         3, 4, 1, 4, 3, 2, 2, 4, 3, 1),
  yield= c(
    70.5, 73.5, 96.0, 100.9, 49.0, 39.2, 58.8, 68.6, 89.1, 92.1, 117.6,  
    108.7, 63.6, 63.6, 72.5, 84.2, 75.8, 52.4, 82.8, 67.0614, 44.1, 46.6, 
    52.5, 45.3, 63.3, 66.7, 78.2, 67.0, 50.3, 52.8, 65.1, 49.6),
)

Here is one dataset, and I’ll use facet_wrap() to create bar graphs. First, let’s summarize the data.

library(dplyr)
df1= data.frame(df %>%
                group_by(cultivar, fertilizer, irrigation) %>%
                dplyr::summarize(across(c(yield), 
                   fns= list(Mean=~mean(., na.rm= TRUE), 
                               SD= ~sd(., na.rm= TRUE), 
                                n=~length(.),
                               se=~sd(.,na.rm= TRUE) / sqrt(length(.))))))

df1
  cultivar fertilizer     irrigation yield_Mean  yield_SD yield_n  yield_se
1     CV_1         N0  Irrigation_No   56.62500  9.410057       4  4.705028
2     CV_1         N0 Irrigation_Yes   67.85000 14.486430       4  7.243215
3     CV_1         N1  Irrigation_No   88.87500 11.593497       4  5.796748
4     CV_1         N1 Irrigation_Yes   92.87500 24.129011       4 12.064505
5     CV_2         N0  Irrigation_No   44.40000  4.045574       4  2.022787
6     CV_2         N0 Irrigation_Yes   57.57500  7.031536       4  3.515768
7     CV_2         N1  Irrigation_No   65.86535  9.354394       4  4.677197
8     CV_2         N1 Irrigation_Yes   77.80000 14.895637       4  7.447818

Then, I’ll create a bar graph using facet_wrap() to divide panels by irrigation.

ggplot(data=df1, aes(x=cultivar, y=yield_Mean, fill=fertilizer))+
  geom_bar(stat="identity", position="dodge", width=0.9, size=1) +
  scale_fill_manual(values=c("grey75","grey15"))+
  geom_errorbar(aes(ymin=yield_Mean-yield_se, ymax=yield_Mean+yield_se),
                position=position_dodge(0.9), width=0.5) +
  scale_y_continuous(breaks=seq(0,150,30), limits=c(0,150)) +
  facet_wrap(~irrigation) +
  labs(x="Cultivar", y="Grain yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position=c(0.8, 0.88),
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=13, color="Black"),
        legend.background=element_rect(fill="white"),
        axis.line=element_line(linewidth=0.5, colour="black"),
        strip.background=element_rect(color="white",
                                      linewidth=0.5, linetype="solid"))

Now, I want to draw a y-axis border for the ‘Irrigation_Yes’ panel. We can achieve this simply by adding scales="free".

gplot(data=df1, aes(x=cultivar, y=yield_Mean, fill=fertilizer))+
  geom_bar(stat="identity", position="dodge", width=0.9, size=1) +
  scale_fill_manual(values=c("grey75","grey15"))+
  geom_errorbar(aes(ymin=yield_Mean-yield_se, ymax=yield_Mean+yield_se),
                position=position_dodge(0.9), width=0.5) +
  scale_y_continuous(breaks=seq(0,150,30), limits = c(0,150)) +
  facet_wrap(~irrigation, scales="free") +
  labs(x="Cultivar", y="Grain yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position=c(0.8, 0.88),
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=13, color= "Black"),
        legend.background=element_rect(fill="white"),
        axis.line=element_line(linewidth=0.5, colour="black"),
        strip.background=element_rect(color="white",
                                      linewidth=0.5,linetype="solid"))


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.