简体   繁体   English

添加p值和R2 ggplot [跟进]

[英]Add p-value and R2 ggplot [follow-up]

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 1:在stat_poly_eq()计算失败:类型为'closure'的对象不可子集化

2: Computation failed in stat_fit_glance() : object of type 'closure' is not subsettable 2:在stat_fit_glance()计算失败:类型为'closure'的对象不可子集化

Short answer: You need to add 简短答案:您需要添加

formula <- y ~ x

(or whatever you define your formula to be) before you call ggplot (ie before the line that reads p <- ggplot(...) . (或定义公式的方式)在调用ggplot之前(即在读取p <- ggplot(...)的行之前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. “ closure”是R中的一种函数。因此,警告消息“'closure'类型的对象不可子集化”表示您正在运行的任何代码都不希望该对象成为函数。

When we look closely at your code, we see formula = formula in your call to stat_poly_eq and stat_fit_glance . 当我们仔细查看您的代码时,在您调用stat_poly_eqstat_fit_glance看到formula = formula 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. 请注意, formula是R中的一个函数。如果您没有单独定义formula对象,R将使您表示您正在引用formula函数。 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. stat_poly_eq()stat_fit_glance()抱怨,因为他们希望函数中的formula 参数formula类对象,而不是函数。

More generally, you shouldn't name your formulas "formula" because it creates confusion. 更一般而言,您不应该将公式命名为“公式”,因为它会引起混淆。 You could use eg "model" instead. 您可以使用“模型”代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM