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


It is a follow-up <a href="https://stackoverflow.com/questions/37536950/ggplot2-add-p-values-to-the-plot/39331729#39331729" rel="nofollow">question</a>. 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!


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(...).

<hr />

<a href="http://adv-r.had.co.nz/Functional-programming.html#closures" rel="nofollow">A "closure" is a type of function in R.</a> 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 <em>argument</em> 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.


  • String Index Out Of Range When Reading Text File
  • What are your recommendations on a jQuery custom select element plugin?
  • How to remove ticks and labels of dropped off factors in a box plot
  • How to communicate with serial LTE modem via Python and Raspberry Pi
  • Minify Code - Java
  • HTTPWebResponse Response string is truncated
  • Output buffer
  • What is the absolute fastest way to implement a concurrent queue with ONLY one consumer and one prod
  • Why can't I write a function with the same type as Box::new?
  • GAE Starting dynamic instance to serve requests instead of using idle resident instances
  • Unable to push Sample Shiny App to Bluemix
  • Does any make variant support dependency filename with colon?
  • jQuery - Internet Explorer memory leaks
  • match + RefCell = X does not live long enough
  • logstash : Mutate { gsub … } not working
  • “Web Services” Node is absent in GlassFish Server Open Source Edition 3.0.1
  • Enable Async TransactionScope without TransactionScopeAsyncFlowOption.Enabled
  • New dataframe column as function (digest) of another one is not working for me
  • Starting a syn region with a keyword
  • C++ Trouble with operator code inheritage: am I require to copy same code for all derived classes?
  • Which PHP RPC (XML or JSON) library have you successfully used?
  • Recursion in F# | What's happening?
  • Threading lock in python not working as desired
  • How would you model data variables variance on common scheme? SQL
  • Colour specific node in igraph
  • AWS SES Production Access setup with Meteor
  • JavaFX icons randomly disappear from TreeTableView when scrolling, performance slow as well
  • Does cast away const of *this cause undefined behavior?
  • How do I control the soft menu button in Honeycomb?
  • Plot a table with R
  • JFreeChart heap space
  • Hudson dependencies
  • R Leaflet Legend: specify order instead of alphabetical
  • Gforce min not supported for character in data.table
  • Show records ordered with maximum price first in PHP & MySQL
  • Find JSON nested nodes using multiple string values
  • Hector: how to query parts of a Composite Type
  • converting text file into xml using php?
  • Generate random number from custom distribution
  • JavaScriptCore crash on iOS9