0

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

Community
  • 1
  • 1
Aby
  • 167
  • 1
  • 3
  • 16
  • Note that if you've already loaded `dplyr` with `library(dplyr)`, there is no need to prepend `dplyr::` (e.g. `dplyr::filter` etc.) when you are calling functions from `dplyr`. – Weihuang Wong Sep 05 '16 at 16:17

1 Answers1

3

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.

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • Great, thanks! I get two R2 and p-values. Why is p-value coming out to be zero? This is also very weird, I tried with changing digits but still doest not work for most columns `signif(..p.value.., digits = 4)` – Aby Sep 05 '16 at 15:58
  • 2
    That's because for the model you're fitting, the p-value is so small that it's close to zero. If you have the `broom` package installed, do `broom::glance(lm(y ~ x - 1, df))` and you'll see that it reports a P-value of zero. – Weihuang Wong Sep 05 '16 at 16:07
  • Where shall I mention `broom::glance(lm(y ~ x - 1, df))`? Before the line that reads `p<- ggplot(..)`? – Aby Sep 05 '16 at 16:12
  • 1
    You can issue the command at any point after you've defined `df`. (Note that `df` itself is a function -- it's the density function for the F-distribution -- so you may not want to call your objects "df" too.) – Weihuang Wong Sep 05 '16 at 16:16
  • Does not work here; either shows up same plot (for sample data) or gives an error `Error in eval(expr, envir, enclos) : object 'y' not found` (for real data). How about incorporating this - `if (p<0.002) star='**' else if (p>=0.002&p<0.05) star='*' else star=''` I used this code for plotting another chart but not sure how it will work with `stat_poly_eq` & `stat_fit_glance` – Aby Sep 05 '16 at 16:25
  • 2
    If you have a follow-up please open a new question; the comments section is not designed for extended discussion. – Weihuang Wong Sep 05 '16 at 16:33