R 函数内的ggplot2带有镶嵌面_包裹

R 函数内的ggplot2带有镶嵌面_包裹,r,function,plot,ggplot2,facet-wrap,R,Function,Plot,Ggplot2,Facet Wrap,我想在自定义函数中使用ggplot2,然后是facet_wrap。问题是,在添加facet\u wrap之前,我的函数会复制我想要的箱线图。下面是一个示例数据帧和基于函数的代码I: # Example dataframe df<-structure(list(tree_number = c(11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 500L, 500L, 500L, 500L, 500L, 500L, 50

我想在自定义函数中使用ggplot2,然后是facet_wrap。问题是,在添加facet\u wrap之前,我的函数会复制我想要的箱线图。下面是一个示例数据帧和基于函数的代码I:

# Example dataframe
        df<-structure(list(tree_number = c(11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 500L, 500L, 500L, 500L, 500L, 500L, 500L, 
    500L, 500L, 500L, 500L, 500L, 504L, 504L, 504L, 504L, 504L, 504L, 
    504L, 504L, 504L, 504L, 504L, 504L, 504L, 504L, 504L, 504L, 504L, 
    504L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L), percent_N = c(3.82, 2.33, 2.63, 2.63, 2.82, 
    2.59, 3.26, 3.18, 2.84, 2.67, 1.62, 1.47, 1.26, 1.62, 1.82, 1.52, 
    1.59, 1.56, 1.55, 1.53, 1.64, 1.71, 1.98, 1.94, 3.06, 2.71, 3.21, 
    3.1, 3.22, 3.68, 3.71, 3.3, 3.22, 3.08, 3.27, 2.69, 3.12, 3.17, 
    2.58, 3.01, 1.43, 1.98, 1.35, 1.85, 1.54, 1.6, 1.44, 1.72, 1.22, 
    1.92, 1.63, 1.76, 1.61, 1.65, 1.55, 1.46, 1.48, 1.47), Leaf.Age = structure(c(3L, 
    3L, 1L, 3L, 1L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
    2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 3L, 2L, 3L, 2L, 
    1L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 3L), .Label = c("1", "2", "3"
    ), class = "factor")), .Names = c("tree_number", "percent_N", 
    "Leaf.Age"), row.names = c(NA, 58L), class = "data.frame")
    sub_title<-paste("All trees percent N")

# ggplot boxplots I want to make a function for
    bn<-ggplot(data=df, aes(x=Leaf.Age, y=percent_N, fill=Leaf.Age)) + 
      geom_boxplot() +
      scale_fill_manual(values=c("yellow", "forest green", "tan4")) +
      theme(axis.text.x=element_text(angle = -90, hjust = 0)) +
      theme_bw() +
      theme(#panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.text.x = element_text(size = 11),
        axis.text.y = element_text(size = 11),
        legend.title = element_blank(),
        legend.text = element_text(size=12))
    bnt<-bn+ggtitle(sub_title)+
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
#示例数据帧
df
lf_age_each_tree<-function(Data,lf_age,Y,Fill){
  B<-ggplot(data=Data, aes(x=lf_age, y=Y, fill=Fill)) + 
    geom_boxplot() +
    scale_fill_manual(values=c("yellow", "forest green", "tan4")) +
    theme(axis.text.x=element_text(angle = -90, hjust = 0)) +
    theme_bw() +
    theme(#panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(),
      axis.text.x = element_text(size = 11),
      axis.text.y = element_text(size = 11),
      legend.title = element_blank(),
      legend.text = element_text(size=12))
  B2<-B+ggtitle(sub_title)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
}
# Facet_wrap applied to original code
bnt2<-bnt+facet_wrap(~tree_number)
bnt2
# versus facet_wrap applied to plot produced from function
boxplot_N<-lf_age_each_tree(df, df$Leaf.Age, df$percent_N, df$Leaf.Age)
boxplot_tree_N<-boxplot_N+facet_wrap(~tree_number)
boxplot_tree_N