I want to use ggplot2 within a custom function, followed by facet_wrap. The problem is that my function reproduces the boxplot that I want until I add facet_wrap. Here is an example dataframe and the code I based the function on:
# 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))
Here is the function to reproduce the plot above:
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))
}
Now here is where I am perplexed. Compare these two 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
I'm stumped why 'bnt2' and 'boxplot_tree_N' are different. Yet 'boxplot_N' and 'bnt' are the same. I've tried aes_string() as described here ggplot2 variables within function but the same problem occurs. I've also tried converting tree_number to a factor. No success so far. Here is the boxplot that I ultimately want.
Note that I know I should not use the df$ within aes() in ggplot2 (as explained here). However, I want to use this function with different dataframes that have similar structure but different column names, and so far the only solution I have found to use this function with different dataframes is to call df$ within aes().