0

I would like to get my statistical test results integrated to my plot. Example of my script with dummy variables (dummy data below generated after first post):

cases <- rep(1:1:5,times=10)
var1 <- rep(11:15,times=10)
outcome <- rep(c(1,1,1,2,2),times=10)

maindata <- data.frame(cases,var1,outcome)

df1 <- maindata %>%
  group_by(cases) %>%
  select(cases,var1,outcome) %>%
  summarise(var1 = max(var1, na.rm = TRUE), outcome=mean(outcome, na.rm =TRUE))

wilcox.test(df1$var1[df1$outcome<=1], df1$var1[df1$outcome>1])

ggplot(df1, aes(x = as.factor(outcome), y = as.numeric(var1), fill=outcome)) + geom_boxplot()

With these everything works just fine, but I can't find a way to integrate my wilcox.test results to my plot automatically (of course I can make use annotation() and write the results manually but that's not what I'm after.

My script produces two boxplots with max-value of var1 on the y-axis and grouped by outcome on the x-axis (only two different values for outcome). I would like to add my wilcox.test results to that boxplot, all other relevant data is present. Tried to find a way from forums and help files but can't find a way (at least with ggplot2)

I'm new to R and trying learn stuff through using ggplot2 and dplyr which I see as most intuitive packages for manipulation and visualization. Don't know if they are optimal for the solution which I'm after so feel free to suggest solutions from alternative packages also...

Zelus1
  • 27
  • 1
  • 6
  • 4
    Would you mind turning your question into a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). All you've got to do is create some dummy data. That would help people to answer your question. – Richard Erickson Apr 14 '15 at 02:20
  • Do you mean that you just want to add the output of printing your wilcox test results as text to the plot? In that case I would try `geom_text` with `label = paste(capture.output(wt), collapse = "\n")` where `wt` is the variable that stores your wilcox test results. What xy coordinates do you want it to appear at? – konvas Apr 14 '15 at 09:45
  • Actually I just want to print out wilcox.test p-value to the plot for example like that (http://ajplung.physiology.org/content/287/2/L332/F5.large.jpg?width=800&height=600&carousel=1). It could be located on top of the boxplots or below the legend. Main point is that it would be visually obvious that it means the statistical test's results between the groups. – Zelus1 Apr 14 '15 at 13:01

1 Answers1

0

I thinks this figure shows what you want. I also added some parts to the code because you're new with ggplot2. Take or leave them, but there're things I do make publication quality figures:

wtOut = wilcox.test(df1$var1[df1$outcome<=1], df1$var1[df1$outcome>1])
exampleOut <- ggplot(df1, 
     aes(x = as.factor(outcome), y = as.numeric(var1), fill=outcome)) + 
     geom_boxplot() + 
     scale_fill_gradient(name = paste0("P-value: ", 
                                       signif(wtOut$p.value, 3), "\nOutcome")) +
     ylab("Variable 1") + xlab("Outcome") + theme_bw()

ggsave('exampleOut.jpg', exampleOut, width = 6, height = 4)

enter image description here

If you want to include the p-value as its own legend, it looks like it is some work, but doable.

Or, if you want, just throw signif(wtOut$p.value, 3) into annotate(...). You'll just need to come up with rules for where to place it.

Community
  • 1
  • 1
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39