热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在每个方面添加两个回归线方程和R2?-howtoaddtworegressionlineequationsandR2switheachfacet?

IwouldliketoaddtworegressionlineequationsandR2switheachfacet.IadoptedJaydensfuncti

I would like to add two regression line equations and R2s with each facet. I adopted Jayden's function to solve the problem, but I found that each equation was the same. The likely reason is sending wrong subset of data to the function. Any advice will be appreciated!

我想在每个方面添加两个回归线方程和R2。我采用Jayden的功能来解决问题,但我发现每个方程都是一样的。可能的原因是向函数发送了错误的数据子集。任何建议将被认真考虑!

My code:

p <- ggplot(data=df,aes(x=x))+
  geom_point(aes(y = y1),size=2.0)+
  geom_smooth(aes(y = y1),method=lm,se=FALSE,size=0.5,
              fullrange = TRUE)+ # Add regression line;
  annotate("text",x = 150,y =320, label = lm_eqn(lm(y1~x,df)), # maybe wrong
           size = 2.0, parse = TRUE)+ # Add regression line equation;

  geom_point(aes(y = y2),size=2.0)+
  geom_smooth(aes(y = y2),method=lm,se=FALSE,size=0.5,
              fullrange = TRUE)+ # Add regression line;
  annotate("text",x = 225,y =50, label = lm_eqn(lm(y2~x,df)),
           size = 2.0, parse = TRUE)+ # Add regression line equation;
  facet_wrap(~trt)

My dataframe:

x   y1  y2  trt
22.48349    34.2    31.0    6030
93.52976    98.5    96.0    6030
163.00984   164.2   169.8   6030
205.62072   216.7   210.0   6030
265.46812   271.8   258.5   6030
23.79859    35.8    24.2    6060
99.97307    119.4   90.6    6060
189.91814   200.8   189.3   6060
268.10060   279.5   264.6   6060
325.65609   325.7   325.4   6060
357.59726   353.6   353.8   6060

My plot: enter image description here

PS. There are two lines and your equations in each facet, and the two lines are right, but the two equations are wrong. Obviously, upper/lower equations in the right and left facet should be different to each other.

PS。每个方面都有两条线和你的方程,两条线是正确的,但这两个方程是错误的。显然,右侧和左侧小平面的上/下方程应该彼此不同。

1 个解决方案

#1


3  

1) ggplot2 Try converting df to long form first (see ## line). We create an annotation data frame ann which defines the text and where it goes for use with geom_text. Note that since the plot is faceted by trt, geom_text will use the trt column in each row of ann to associate that row with the appropriate facet.

1)ggplot2首先尝试将df转换为长格式(参见## line)。我们创建了一个注释数据框ann,它定义了文本以及它与geom_text一起使用的位置。请注意,由于绘图是由trt分割的,因此geom_text将使用ann的每一行中的trt列将该行与相应的facet相关联。

library(ggplot2)
library(reshape2)

long <- melt(df, measure.vars = 2:3) ##

trts <- unique(long$trt)
ann <- data.frame(x = c(0, 100), 
                  y = c(250, 100), 
                  label = c(lm_eqn(lm(y1 ~ x, df, subset = trt == trts[1])), 
                            lm_eqn(lm(y2 ~ x, df, subset = trt == trts[1])),
                            lm_eqn(lm(y1 ~ x, df, subset = trt == trts[2])), 
                            lm_eqn(lm(y2 ~ x, df, subset = trt == trts[2]))),
                  trt = rep(trts, each = 2),
                  variable = c("y1", "y2"))

ggplot(long, aes(x, value)) +
       geom_point() + 
       geom_smooth(aes(col = variable), method = "lm", se = FALSE,
              full_range = TRUE) + 
       geom_text(aes(x, y, label = label, col = variable), data = ann, 
             parse = TRUE, hjust = -0.1, size = 2) +
       facet_wrap(~ trt)

ann could equivalently be defined like this:

ann可以等效地定义如下:

f <- function(v) lm_eqn(lm(value ~ x, long, subset = variable==v[[1]] & trt==v[[2]]))
Grid <- expand.grid(variable = c("y1", "y2"), trt = trts)
ann <- data.frame(x = c(0, 100), y = c(250, 100), label = apply(Grid, 1, f), Grid)

(continued after image)

(图片后继续)

screenshot

2) lattice Its possibly easier in this case with lattice:

2)格子在这种情况下格子可能更容易:

library(lattice)

xyplot(y1 + y2 ~ x | factor(trt), df, 
       key = simpleKey(text = c("y1", "y2"), col = c("blue", "red")),
       panel = panel.superpose, 
       panel.groups = function(x, y, group.value, ...) {
          if (group.value == "y1") {
              X <- 150; Y <- 300; col <- "blue"
          } else {
              X <- 250; Y <- 100; col <- "red"
          }
          panel.points(x, y, col = col)
          panel.abline(lm(y ~ x), col = col)
          panel.text(X, Y, parse(text = lm_eqn(lm(y ~ x))), col = col, cex = 0.7)
       }
)

(continued after image)

(图片后继续)

screenshot

3) latticeExtra or we could make the lattice plot more ggplot2-like:

3)latticeExtra或者我们可以使格子图更像ggplot2:

library(latticeExtra)

xyplot(y1 + y2 ~ x | factor(trt), df, par.settings = ggplot2like(),
       key = simpleKey(text = c("y1", "y2"), col = c("blue", "red")),
       panel = panel.superpose, 
       panel.groups = function(x, y, group.value, ...) {
          if (group.value == "y1") {
              X <- 150; Y <- 300; col <- "blue"
          } else {
              X <- 250; Y <- 100; col <- "red"
          }
          panel.points(x, y, col = col)
          panel.grid()
          panel.abline(lm(y ~ x), col = col)
          panel.text(X, Y, parse(text = lm_eqn(lm(y ~ x))), col = col, cex = 0.7)
       }
)

(continued after image)

(图片后继续)

screenshot

Note: We used this as df:

注意:我们将其用作df:

df <-
structure(list(x = c(22.48349, 93.52976, 163.00984, 205.62072, 
265.46812, 23.79859, 99.97307, 189.91814, 268.1006, 325.65609, 
357.59726), y1 = c(34.2, 98.5, 164.2, 216.7, 271.8, 35.8, 119.4, 
200.8, 279.5, 325.7, 353.6), y2 = c(31, 96, 169.8, 210, 258.5, 
24.2, 90.6, 189.3, 264.6, 325.4, 353.8), trt = c(6030L, 6030L, 
6030L, 6030L, 6030L, 6060L, 6060L, 6060L, 6060L, 6060L, 6060L
)), .Names = c("x", "y1", "y2", "trt"), class = "data.frame", row.names = c(NA, 
-11L))

Update

  • Added colored text.
  • 添加了彩色文字。

  • Added alternate ann.
  • 添加了备用安。

  • Added lattice solution.
  • 添加晶格解决方案

  • Added latticeExtra variation to the lattice solution.
  • 为晶格解决方案添加了latticeExtra变体。


推荐阅读
author-avatar
kaxiaoliog_334
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有