I have approximately 800 plots to do, and am using ggplot facet wrap. I would like to insert the equation in the strip at the top of each plot. I have come across this code (Adding R^2 on graph with facets), and have been trying to use this, but I get a warning stating that "The labeller API has been updated. Labellers taking variable
and value
arguments are now deprecated. See labellers documentation". I have had no luck in trying to resolve this, and am hoping someone can help me. Below is an example of my code in a minimum realistic example. Note, I will be using facet_wrap_paginate after for the real data to produce the series of plots I need--the real data spans several orders of magnitude so defining a position for the equation on the plot will not work.
Thank-you for your help!
Group <- c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C")
Year <- c(1,2,3,4,1,2,3,4,1,2,3,4)
Qty <- c(4.6, 4.2, 4.0, 3.9, 6.4, 6.0, 6.3, 5.9, 5.3, 5.2, 4.3, 4.1)
#create data frame
df <- data.frame (Group, Year, Qty)
#set up equattions for facet wrap strip
lm_eqn = function(df)
{
m = lm(Qty ~ Year, data = df);
eqns <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eqns));
}
#Create object made up of equations for each Group
eqns <- by(df, df$Group, lm_eqn)
#Create object to add to strip
eqnlabels <- function(variable, value)
{
return(eqns)
}
#Make plots
df_Plots <- ggplot (data = df, aes(x = Year, y = Qty, group = Group)) +
geom_point(data = df, shape = 1, color = "blue") +
scale_x_continuous (breaks = seq (from = 0, to = 4, by = 1)) +
geom_smooth(method = "lm", formula = y~x, se = FALSE, linetype = 1, color = "black") +
theme(legend.position = "none", panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(),
strip.background = element_blank(), panel.background = element_rect(fill = 'white')) +
annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf, size = 0.5) +
facet_wrap(~Group, labeller = eqnlabels) +
#Set up page of graphs
facet_wrap_paginate(~Group, ncol = 2, nrow = 3, scales = "free_y")
Plotz <- df_Plots
print(Plotz)
...