I am trying to create a ggplot
that will show the result of a ttest on the plot. I am now using the line annotate("text",x=1,y=10,label='atop(bold("P-value = 0.286"))',cex=7,parse=TRUE)
. The only problem is that I have to change the value manually each time the test results change. I would like to insert ttest$p.value
instead of the number in the label but keep it in bold.
Any ideas?
Asked
Active
Viewed 809 times
1

B.Shermeister
- 507
- 2
- 6
- 17
-
Check out [`ggpubr`](http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/), it should be able to do this for you and has lots of examples – camille May 04 '18 at 18:52
-
When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 04 '18 at 20:19
1 Answers
0
You can just use a paste statement to help put your dynamic p-value into the annotate step. The example here is super ugly, but you will get the idea, just replace my p_val in the annotate step with your ttest$p.value:
rawdata <- data.frame('var1' = runif(100,1,100),
'var2' = runif(100,1,100))
library(ggplot2)
p_val <- .286
ggplot(rawdata,aes(x=1:100,y=var1)) + geom_line() +
annotate("text",x=50,y=10,label=paste0('atop(bold("p_value is ',p_val,'"))'),cex=7,parse=TRUE)

Nate Thompson
- 625
- 1
- 7
- 22