R ggplot:将方程式添加到镶嵌面包裹条
我有大约800个绘图要做,我正在使用ggplot facet wrap。我想在每个图顶部的条形图中插入方程式。我遇到了这段代码(),并一直试图使用它,但我收到一条警告,指出“labeller API已更新。使用R ggplot:将方程式添加到镶嵌面包裹条,r,ggplot2,equation,facet-wrap,R,Ggplot2,Equation,Facet Wrap,我有大约800个绘图要做,我正在使用ggplot facet wrap。我想在每个图顶部的条形图中插入方程式。我遇到了这段代码(),并一直试图使用它,但我收到一条警告,指出“labeller API已更新。使用变量和值参数的labeller现在已被弃用。请参阅labeller文档”。我没有运气解决这个问题,我希望有人能帮助我。下面是我的代码的一个最现实的例子。注意,对于真实数据,我将使用facet_wrap_paginate after生成我需要的一系列图——真实数据跨越几个数量级,因此在图上定
变量
和值
参数的labeller现在已被弃用。请参阅labeller文档”。我没有运气解决这个问题,我希望有人能帮助我。下面是我的代码的一个最现实的例子。注意,对于真实数据,我将使用facet_wrap_paginate after生成我需要的一系列图——真实数据跨越几个数量级,因此在图上定义方程的位置将不起作用
谢谢你的帮助
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)
组