在ggplot2中使用stat_poly_eq指定每个构面的公式 [英] Specifying formula for each facet using stat_poly_eq in ggplot2

查看:3440
本文介绍了在ggplot2中使用stat_poly_eq指定每个构面的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从

解决方案

您可以单独更新面板6的公式(当然,您也可以使用类似功能更新所有面板,但这里没有必要)

  rename_panel_expression<  - 函数(grb,panel,expr){
g< - grb $ grobs [[panel + 1]] $ children
grb $ grobs [[panel + 1]] $ children [[grep(GRID.text,names(g))]] $ label< - expr
grb
}

l < - lm(mpg〜log(disp),mtcars [mtcars $ am == 1& mtcars $ gear == 5,])

tt< ; - rename_panel_expression(ggplotGrob(p),6,
bquote(italic(y)〜`=`〜。(round(l $ coefficients [1],3)) - (圆)(概要(1)$ r.squar(2)),3))*〜斜体(x)~~斜体(R)^ 2 =`= ed),3))))

grid :: grid.newpage()
grid :: grid.draw(tt)


I borrowed this example dataset from here:

# Load library
library(ggplot2)

# Load data
data(mtcars)

# Plot data
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth(method="lm")
print(p)

In above code the regression methods and formulae are the same in all facets. If we want to specify formula for facet (or panel) 6, we have the following code, from here:

# Smoothing function with different behaviour depending on the panel
custom.smooth <- function(formula, data,...){
  smooth.call <- match.call()

  if(as.numeric(unique(data$PANEL)) == 6) {
    # Linear regression
    smooth.call[[1]] <- quote(lm)
    # Specify formula
    smooth.call$formula <- as.formula("y ~ log(x)")
  }else{
    # Linear regression
    smooth.call[[1]] <- quote(lm)
  }

  # Perform fit
  eval.parent(smooth.call)
}

# Plot data with custom fitting function
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth(method = "custom.smooth", se = FALSE)
print(p)

Now if I want to add regression equations to these facets:

# Load library
library(ggpmisc)
p + stat_poly_eq(formula = y ~ x,aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
    parse=TRUE,label.x.npc = "right")

Then what should I do, to specify the equation and R2 displayed on panel 6, that can match the model I specified before? See the plot below, now panel 6 has its own fitting model, but the equation label doesn't. Maybe we can define a similar function as we did to ggplot2 parameters?

解决方案

You could update panel 6's formula individually (of course you could also update all panels with a function like that, but there's no need for that here)

rename_panel_expression <- function(grb, panel, expr) {
  g <- grb$grobs[[panel + 1]]$children
  grb$grobs[[panel + 1]]$children[[grep("GRID.text", names(g))]]$label <- expr
  grb
}

l <- lm(mpg ~ log(disp), mtcars[mtcars$am == 1 & mtcars$gear == 5, ])

tt <- rename_panel_expression(ggplotGrob(p), 6, 
  bquote(italic(y)~`=`~.(round(l$coefficients[1], 3)) - .(round(abs(l$coefficients[2]), 3))*~italic(x)~~~italic(R)^2~`=`~.(round(summary(l)$r.squared, 3))))

grid::grid.newpage()
grid::grid.draw(tt)

这篇关于在ggplot2中使用stat_poly_eq指定每个构面的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆