ggplot:将方程式添加到分面包装条

问题描述

我大约有800幅图要做,并且正在使用ggplot多面包装。我想在每个图的顶部的条中插入方程式。我遇到了这段代码Adding R^2 on graph with facets),并一直在尝试使用它,但是我收到一条警告,指出“标签程序API已更新。标签程序采用variablevalue现在不推荐使用参数。请参见标签制作者文档”。我没有运气来解决这个问题,希望有人能帮助我。下面是我的代码一个最小现实示例。请注意,之后我将使用facet_wrap_paginate生成真实数据,以生成所需的一系列绘图-实际数据跨几个数量级,因此无法在绘图上为方程式定义位置。

谢谢您的帮助!

Group <- c("A","A","B","C","C")
Year <- c(1,2,3,4,1,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],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)

...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)