2

I try to plot my data using this R code:

print(ggplot(data = my_data3, aes(x = Visit, y = Variable1, group = number)) + 
     geom_point(aes(col=Treatment), size = 2) +
     geom_line(aes(col=Treatment)) +
     facet_grid(. ~ Treatment) +
     ggtitle("Variable1")+
     theme_bw() + 
     stat_compare_means(comparisons = list(c("visit 1", "visit 2")), label = "p.format", method = "wilcox.test", paired=T, tip.length = 0))

My Variable3 contains 2 variables that are plotted as 2 graphs when I use the facet_grid function. However, the p value is only shown for one of the plots. How can I get the p value for both plots?Graph showing the p value for 1 facet

This is part of the dataset:

my_data3 <- structure(list(number = c(110002, 110002, 110003, 110003, 110004, 
                                 110004, 110005, 110005, 110006, 110006, 110007, 110007, 110008, 
                                 110008, 110009, 110009, 110010, 110010, 110011, 110011, 110012, 
                                 110012, 110013, 110013, 110014, 110014, 110016, 110016, 110017, 
                                 110017), Treatment = c("Treatment1", "Treatment1", "Treatment2", 
                                                        "Treatment2", "Treatment2", "Treatment2", "Treatment2", "Treatment2", 
                                                        "Treatment1", "Treatment1", "Treatment1", "Treatment1", "Treatment2", 
                                                        "Treatment2", "Treatment2", "Treatment2", "Treatment1", "Treatment1", 
                                                        "Treatment2", "Treatment2", "Treatment1", "Treatment1", "Treatment2", 
                                                        "Treatment2", "Treatment2", "Treatment2", "Treatment1", "Treatment1", 
                                                        "Treatment2", "Treatment2"), Visit = c("visit 1", "visit 2", 
                                                                                               "visit 1", "visit 2", "visit 1", "visit 2", "visit 1", "visit 2", 
                                                                                               "visit 1", "visit 2", "visit 1", "visit 2", "visit 1", "visit 2", 
                                                                                               "visit 1", "visit 2", "visit 1", "visit 2", "visit 1", "visit 2", 
                                                                                               "visit 1", "visit 2", "visit 1", "visit 2", "visit 1", "visit 2", 
                                                                                               "visit 1", "visit 2", "visit 1", "visit 2"), Variable1 = c(5618, 
                                                                                                                                                          4480.5, 1034.75, 706.75, 11492.5, 6037.5, 3841.5, 2762.75, 306, 
                                                                                                                                                          138.5, 259.5, 0, 31.5, 911.75, 1909.5, 1352.75, 1957.75, 2383.25, 
                                                                                                                                                          23538.25, 8595.5, 13360.5, 10337.5, 1696.5, 805.25, 14655, 6169, 
                                                                                                                                                          10141, 5922.25, 2164.25, 14990.25)), .Names = c("number", "Treatment", 
                                                                                                                                                                                                          "Visit", "Variable1"), row.names = c(NA, 30L), class = "data.frame")
Irma
  • 21
  • 1
  • 4
  • It would be great if you could make your question reproducible by providing an example dataset or using `dput` on `my_data3`. That having been said, I think you could use `compare_means` instead of `stat_compare_means` -- `compare_means` has a `group.by` argument. So something like: `compare_means(Variable1 ~ Visit, group.by = number)` might work. – JasonAizkalns Feb 26 '19 at 15:18
  • Thank you for your answer. I have now added part of the dataset (not sure if I did it right). I think compare means is not working, I think it is not made to use for plotting the p value? – Irma Feb 26 '19 at 15:48
  • Please do not post screenshots of data. Make your questions reproducible by either using `dput()` to include your data as copy/pasteable text, or by using an included in R or simulated dataframe in your question. See [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jan Boyer Feb 26 '19 at 15:50
  • Sorry for that, I think I added the data correctly now. – Irma Feb 26 '19 at 16:00
  • @JasonAizkalns, could you help me with implementing the compare_means function in my script? I could not find how to combine it with plotting the data in ggplot2. Thanks in advance! – Irma Mar 15 '19 at 14:55

1 Answers1

3

I ran into a similar problem today, so I'll leave the answer here in case anyone else needs it in the future:

It seems that stat_compare_means struggles when you include a grouping variable in the general aesthetics of the plot (using group, color, fill, etc), so you should move these to the aesthetics of the specific function where you want to use them.

For your code, I only had to move the group = number argument inside the geom_line function and the problem was solved:

ggplot(data = my_data3, aes(x = Visit, y = Variable1)) + 
  geom_point(aes(col=Treatment), size = 2) +
  geom_line(aes(col=Treatment, group = number)) +
  facet_grid(. ~ Treatment) +
  ggtitle("Variable1")+
  theme_bw() + 
  stat_compare_means(comparisons = list(c("visit 1", "visit 2")), 
  label = "p.format", method = "wilcox.test", paired=T, tip.length = 0)

fixed plot with pvalues in both facets

magdalena
  • 46
  • 1
  • 1
  • 8