2
votes

I have a sample data frame as follows:

demo = data.frame(percent = c(84.9,71.4,82.6,69.0,94.1,94.8,91.6,86.5,21.4,70.7,92.3,94.4,28.8,21.8,93.7,87.2),
                 status = rep(c("Pre","Pre","Pre","Pre","Post","Post","Post","Post"),2),
                 gender = c(rep("Male",8),rep("Female",8)),
                 id = c(rep(c("1","2","3","4"),2),rep(c("5","6","7","8"),2)))

I then proceed to facet the data frame by gender and make a paired plot for each gender using status as the x variable and percent as the y variable using the following codes:

compare = list(c("Pre","Post"))
demo %>% ggplot(aes(x=factor(status,c("Pre","Post")),y=percent,group=id)) + ylim(0,101) +
  geom_point(size = 2, aes(color = status)) + geom_line() + 
  facet_grid(~ gender,switch = "x") +
  theme(legend.position = "none",
        axis.title.x = element_blank(),
        strip.placement = "outside",
        strip.text.x = element_text(angle=0)) +
  stat_compare_means(comparisons = compare,label="p.signif",
                     method = "t.test",paired=T,label.y=100.5,label.x = 1.5,tip.length=0)

Yet the output of this plot only has the asterisk and bracket for the Male group but not the Female group, but I want it to also have a bracket showing "NS" as label, I was wondering why the bracket disappeared? (p.s. I've also tried the hide.ns argument but it didn't work). It now looks like this: enter image description here

1

1 Answers

0
votes

For your plot, one solution might be to call ggline followed by stat_compare_means() :

demo %>% 
mutate(status = relevel(status,ref="Pre")) %>%
ggline(x = "status",y="percent",group="id",
facet.by="gender",point.color="status") + 
stat_compare_means(method="t.test",aes(label=..p.signif..),
paired=TRUE,comparisons=compare)

enter image description here