Search
 
SCRIPT & CODE EXAMPLE
 

R

linear regression r text label coefficient ggplot

# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: https://groups.google.com/forum/#!topic/ggplot2/1TgH-kG5XMA

lm_eqn <- function(df){
    m <- lm(y ~ x, df);
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
         list(a = format(unname(coef(m)[1]), digits = 2),
              b = format(unname(coef(m)[2]), digits = 2),
             r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));
}

p1 <- p + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)
Comment

linear regression r text label coefficient ggplot

lm_eqn = function(m) {

  l <- list(a = format(coef(m)[1], digits = 2),
      b = format(abs(coef(m)[2]), digits = 2),
      r2 = format(summary(m)$r.squared, digits = 3));

  if (coef(m)[2] >= 0)  {
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
  } else {
    eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)    
  }

  as.character(as.expression(eq));                 
}
Comment

PREVIOUS NEXT
Code Example
R :: SSL certificate problem in R 
R :: ggplot2 color gradient 
R :: how to rename variables in r 
R :: ways to examine model in r 
R :: change font color in geom_text in ggplot2 in R 
R :: combine row for every element of vector r 
R :: legend in r 
R :: r while loop 
Rust :: hello world rust 
Rust :: array as a parameter rust 
Rust :: rust absolute value 
Rust :: rust null 
Rust :: host rust server 
Rust :: length of vector rust 
Rust :: rust char array 
Rust :: armanriazi•rust•Rc 
Rust :: rust initialize empty array 
Rust :: Repeat the given string exactly n times 
Rust :: armanriazi•rust•error•the trait `Binary` is not implemented for `f64` 
Rust :: armanriazi•rust•error•E0220•associated type `` not found for `Self` 
Rust :: get value from option rust 
Rust :: armanriazi•rust•concept•unrolling 
Rust :: armanriazi•rust•comparison•generic•associated type 
Lua :: lua how to get random object from a table 
Lua :: luau kill brick script 
Lua :: lua drawinrect 
Lua :: how to check if a sting incules something roblox 
Lua :: roblox number generator 
Lua :: countif excel 
Lua :: FiveM Lua How to create table of all online player id 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =