使用facet_wrap的函数中的ggplot2

时间:2016-01-18 21:51:29

标签: r function plot ggplot2 facet-wrap

我想在自定义函数中使用ggplot2,然后是facet_wrap。问题是我的函数再现了我想要的箱图,直到我添加了facet_wrap。这是一个示例数据帧和基于该函数的代码:

# 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))

以下是重现上图的功能:

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_wraps:

# 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

我很难过为什么'bnt2'和'boxplot_tree_N'不同。然而'boxplot_N'和'bnt'是相同的。我已经尝试了如此处所述的aes_string() ggplot2 variables within function 但同样的问题发生了。我也尝试将tree_number转换为一个因子。到目前为止没有成功。这是我最终想要的箱线图。

Boxplot I want with facet_wrap

请注意,我知道我不应该在ggplot2中的aes()中使用df $(如here所述)。但是,我希望将此函数与具有相似结构但列名不同的不同数据帧一起使用,到目前为止,我发现在不同的数据帧中使用此函数的唯一解决方案是在aes()中调用df $。

0 个答案:

没有答案