0

how can I run t-test with the mu-argument = "greater than" in dplyr, by group? here my data frame

structure(list(Lattobacilli = c(6.39794000867204, 4.91381385238372, 
7.7160033436348, 7.91907809237607, 6.6232492903979, 7.63346845557959, 
7.27875360095283, 7.43136376415899, 5.54406804435028, 6.36172783601759, 
4.55630250076729, 8.38021124171161, 7.94939000664491, 7.04139268515823
), Starter = structure(c(3L, 3L, 6L, 6L, 7L, 7L, 8L, 8L, 4L, 
4L, 4L, 5L, 5L, 5L), .Label = c("B", "C", "Ch1", "Ds1", "Ds2", 
"Sa1", "Sa2", "Sa3"), class = "factor"), Giorni = structure(c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0", 
"1", "7", "35"), class = "factor")), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), .Names = c("Lattobacilli", 
"Starter", "Giorni"), vars = "Starter", drop = TRUE, indices = list(
    0:1, 8:10, 11:13, 2:3, 4:5, 6:7), group_sizes = c(2L, 3L, 
3L, 2L, 2L, 2L), biggest_group_size = 3L, labels = structure(list(
    Starter = structure(3:8, .Label = c("B", "C", "Ch1", "Ds1", 
    "Ds2", "Sa1", "Sa2", "Sa3"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L), vars = "Starter", drop = TRUE, .Names = "Starter"))

I found this solution

lattotest<-df%>% 
select(Lattobacilli,Starter,Giorni)%>% 
filter(Giorni=="35",!is.na(Lattobacilli))%>% 
group_by(Starter)%>% 
mutate(p_value=t.test(Lattobacilli, mu = 6,alternative="greater")$p.value, t_value=t.test(Lattobacilli, mu = 6,alternative="greater")$statistic)

> lattotest
# A tibble: 14 x 5
# Groups:   Starter [6]
   Lattobacilli Starter Giorni p_value t_value
          <dbl> <fct>   <fct>    <dbl>   <dbl>
 1         6.40 Ch1     35      0.638   -0.464
 2         4.91 Ch1     35      0.638   -0.464
 3         7.72 Sa1     35      0.0178  17.9  
 4         7.92 Sa1     35      0.0178  17.9  
 5         6.62 Sa2     35      0.134    2.23 
 6         7.63 Sa2     35      0.134    2.23 
 7         7.28 Sa3     35      0.0179  17.8  
 8         7.43 Sa3     35      0.0179  17.8  
 9         5.54 Ds1     35      0.785   -0.982
10         6.36 Ds1     35      0.785   -0.982
11         4.56 Ds1     35      0.785   -0.982
12         8.38 Ds2     35      0.0226   4.54 
13         7.95 Ds2     35      0.0226   4.54 
14         7.04 Ds2     35      0.0226   4.54

than I would like to add the results in a ggbarplot using stat_pvalue_manual, however, is not possible to add only the p-value but I need to insert also brachets that i do not have because I compared the means to a fixed value, not between groups

tjebo
  • 21,977
  • 7
  • 58
  • 94
Spigonico
  • 137
  • 1
  • 10
  • 1
    @Tjebo I did, thanks – Spigonico Nov 11 '18 at 12:02
  • It is a bit unclear to me what you want to achieve - you would like to plot bar plots with the p-value as a label? What do you mean with "brachets" (brackets?) – tjebo Nov 11 '18 at 12:07
  • @Tjebo yes, i would like to label barplot with the pvalue using ggpubr. ggpubr only admit to add pvalue with comparison between groups not to a single value – Spigonico Nov 11 '18 at 12:11

1 Answers1

1

One option is just to use ggplot with geom_text to annotate. First you need to create a new data frame containing the p-values for each "x"

library(dplyr)
library(ggplot2) 

 # I used your data frame "lattotest"

latto_p <- lattotest %>% group_by(Starter) %>% summarise(p = mean(p_value))

ggplot() +
  geom_bar(data = lattotest, aes(Starter, Lattobacilli), stat = 'identity') +
  geom_text(data = latto_p, aes(label = round(p,3), x = Starter, y = 1))

enter image description here

You can play around by adding specific y-values to your data frame, and you can add whatever text you want by using paste in the label argument

tjebo
  • 21,977
  • 7
  • 58
  • 94