1

I have created a graph with the follow code:

ggplot(data.frame, aes(x=Score, y=Year, col=Position)) + geom_smooth(method="lm", se=FALSE).

The plot is shown below. I'd like to add Pearson r values and P-values if possible to the accompanying label table or a new table on the graph? Adding the P-values is more important than the Pearson r's. Can anyone help me with this?

A sample of the data for reproducibility:

Year   Position   Score
2010   QB         16.5
2011   QB         15.4
2012   QB         16.1
2013   QB         14.3
2014   QB         13.8
2010   RB         14.2
2011   RB         13.9
2012   RB         13.9
2013   RB         11.8
2014   RB         11.6
2010   WR         11.4
2011   WR         12.4
2012   WR         10.4
2013   WR         8.8
2014   WR         9.7

:enter image description here

Evan
  • 1,477
  • 1
  • 17
  • 34
  • 2
    Add some example data to make this a more [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1) – alan ocallaghan Mar 05 '18 at 17:46
  • Good idea, I realized having all positions would be way too much sample data so I just did three positions – Evan Mar 05 '18 at 17:53

1 Answers1

2

Check out ggpmisc::stat_fit_glance()

Without a reproducible example I can't give a perfect code, but it should look something like this.

library(ggpmisc)
ggplot(data.frame, aes(x=Score, y=Year, col=position_new.y)) +
geom_smooth(method="lm", se=FALSE) +
stat_fit_glance(method = 'lm', geom = 'text', aes(label = paste0('p = ', round(..p.value.., 3))), label.x.npc = 0.4, label.y.npc = 1) + 
alan ocallaghan
  • 3,116
  • 17
  • 37
CephBirk
  • 6,422
  • 5
  • 56
  • 74
  • Thanks! I added a reproducible example – Evan Mar 05 '18 at 17:54
  • I got an error `Error in if (is.waive(data) || empty(data)) return(cbind(data, PANEL = integer(0))) : missing value where TRUE/FALSE needed` – Evan Mar 05 '18 at 17:58
  • 1
    It seems that `data` is `NULL` in your case. – alan ocallaghan Mar 05 '18 at 18:01
  • @aocall, yep, it was null. Now my problem is that it is rounding p-values below 0.001 to 0. Is it posisble to have these p-values show up in the table of labels? – Evan Mar 05 '18 at 18:06
  • 1
    Try changing `round(..p.value.., 3)` to `format(..p.value.., 3)` – alan ocallaghan Mar 05 '18 at 18:08
  • @aocall, that put them into scientific notation and now has ~7sigfigs. – Evan Mar 05 '18 at 18:12
  • 1
    Well... Perhaps you can tweak the call to `round` or `format` to produce suitable text? – alan ocallaghan Mar 05 '18 at 18:14
  • @aocall, I'm confused what that means – Evan Mar 05 '18 at 18:16
  • Given that you now know which bit of code formats the labels, can you not devise a solution yourself? – alan ocallaghan Mar 05 '18 at 18:22
  • @aocall, the format I'm looking for is a table on my graph somewhere, or integrated with the table of labels of positions. I don't know which part of code formats those tables, but could manually do it once I get a little push from StackOverflow – Evan Mar 05 '18 at 18:30
  • The p-values shown on the plot are formatted by `round`, since `label` is set to the result of that call. The problem with your questions is that you do not seem to be asking *how*, you are merely asking for us to do your work for you. To change the legend table, look into `scale_color_discrete`. To change how the p-values are formatted, change the call to round. – alan ocallaghan Mar 05 '18 at 21:16
  • For example, you could try this: `stat_fit_glance(method = 'lm', geom = 'text', aes(label = paste0('p = ', ifelse(..p.value.. >= 0.001, round(..p.value.., digits = 3), "< 0.001"))), label.x.npc = 0.4, label.y.npc = 1)` – alan ocallaghan Mar 05 '18 at 21:24