添加p值和R2 ggplot [后续] [英] Add p-value and R2 ggplot [follow-up]

查看:2503
本文介绍了添加p值和R2 ggplot [后续]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续问题 。当我运行下面给出的代码时,我收到警告消息,我认为这是由于我的代码中没有方面要求,而链接中提到的源代码包含方面。看一看,请让我知道哪一部分需要修改。向前看!



代码:

  library(dplyr)
library(ggplot2)
library(ggpmisc)

df < - diamonds%>>%
dplyr :: filter(cut%in%c(Fair, 理想))%>%
dplyr :: filter(清晰度%%c(I1,SI2,SI1,VS2,VS1,VVS2))% >%
dplyr :: mutate(new_price = ifelse(cut ==Fair,
price * 0.5,
price * 1.1))


p <-ggplot(df,aes(x,y,color = factor(cut)))
p <-p + stat_smooth(method =lm,formula = y〜x-1,size = 1,level = 0.95)
p <-p + geom_point(alpha = 0.3)
p <-p + stat_poly_eq(aes(label = paste(.. rr.label ..)),
label.x.npc =right,label.y.npc = 0.15,formula = formula,
parse = TRUE,size = 3)+
stat_fit_glance(method ='lm',method .args = list(公式=公式),
geom =' text',aes(label = paste(P-value =,
signif(.. p.value ..,digits = 4),sep =)),label.x.npc ='right ',
label.y.npc = 0.35,size = 3)
print(p)

警告消息:
$ b <1>: stat_poly_eq()中的计算失败:
对象类型'closure'不可子集

<2>:在 stat_fit_glance()中计算失败:
object类型'closure'不可子集

解决方案

简短回答:您需要添加

 公式<-y〜x 

(或任何你定义你的公式),然后再调用 ggplot (即在读取 p < - ggplot(...)





$ b的行之前$ b

封闭是R中的一种函数。 因此,警告消息类型'闭包'的对象不是子集意味着你运行的任何代码都不期待一个对象是函数。



当我们仔细查看代码时,在调用 stat_poly_eq formula = formula > stat_fit_glance 。请注意,公式是R中的一个函数。如果您没有单独定义公式对象,R会带您意味着你指的是公式函数。 stat_poly_eq() stat_fit_glance()正在抱怨,因为他们期望公式 $ b $函数中的$ c> 参数是一个公式 - 类对象,而不是一个函数。 b

更一般地说,你不应该命名你的公式公式,因为它会造成混淆。您可以使用例如model代替。

It is a follow-up question. When I run the code given below, I get warning message that I think is due to no facets requirement in my code while the source code mentioned in link included facets. Have a look and please let me know which part needs to be amended. Looking forward!

Code:

library(dplyr) 
library(ggplot2)
library(ggpmisc)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))


p <- ggplot(df, aes(x,y, color=factor(cut))) 
p <- p + stat_smooth(method = "lm", formula = y ~ x-1, size = 1, level=0.95) 
p <- p + geom_point(alpha = 0.3) 
p <- p + stat_poly_eq(aes(label = paste(..rr.label..)),
                      label.x.npc = "right", label.y.npc = 0.15, formula = formula, 
                      parse = TRUE, size = 3) + 
          stat_fit_glance(method = 'lm', method.args = list(formula = formula),
                      geom = 'text', aes(label = paste("P-value = ", 
                      signif(..p.value.., digits = 4), sep = "")),label.x.npc = 'right',
                      label.y.npc = 0.35, size = 3)
print(p)

Warning messages:

1: Computation failed in stat_poly_eq(): object of type 'closure' is not subsettable

2: Computation failed in stat_fit_glance(): object of type 'closure' is not subsettable

解决方案

Short answer: You need to add

formula <- y ~ x

(or whatever you define your formula to be) before you call ggplot (i.e. before the line that reads p <- ggplot(...).


A "closure" is a type of function in R. So the warning message "object of type 'closure' is not subsettable" means that whatever code you were running was not expecting an object that's a function.

When we look closely at your code, we see formula = formula in your call to stat_poly_eq and stat_fit_glance. Note that formula is a function in R. If you don't define a formula object separately, R will take you to mean that you are referring to the formula function. stat_poly_eq() and stat_fit_glance() are complaining because they expect the formula argument in the function to be a formula-class object, not a function.

More generally, you shouldn't name your formulas "formula" because it creates confusion. You could use e.g. "model" instead.

这篇关于添加p值和R2 ggplot [后续]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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