1
votes

I'm trying to put together an R ggplot graph of some categorical data with pairwise p-values. I can graph the data, and work out the pairwise p-values, but can't work out how to display them together. Does anyone have any tips?

My preference would be to display them using something like geom_signif(), but I'm completely stuck. At this stage, I would happily accept displaying them as a table below the graph, if that's easier?

Data:

My data is in the following format:

> str(df)
'data.frame':   589 obs. of  2 variables:
 $ grp1: Factor w/ 2 levels "N","Y": 1 2 1 1 1 1 1 2 2 2 ...
 $ grp2: Factor w/ 4 levels "A","B","C","D": 3 1 2 1 3 3 4 2 2 1 ...

The counts of the data are:

> table(df)
    grp2
grp1   A   B   C   D
   N  81 144 117  88
   Y  40  61  38  20

The pairwise proportion tests are:

> library(rstatix)
> pairwise_prop_test(table(df))
# A tibble: 6 x 5
  group1 group2      p p.adj p.adj.signif
* <chr>  <chr>   <dbl> <dbl> <chr>       
1 A      B      0.618  0.948 ns          
2 A      C      0.153  0.612 ns          
3 B      C      0.325  0.948 ns          
4 A      D      0.0189 0.113 ns          
5 B      D      0.0432 0.216 ns          
6 C      D      0.316  0.948 ns          

The graph is:

> library(ggplot2)
> ggplot (df, aes(x=grp2, fill=grp1))+
+ geom_bar(position="fill")

Proportional Bar Graph

enter image description here

Goal:

I want to add the number from the column p of pairwise_prop_test(table(df)) to the graph, either using geom_signif(), or as a table of grp1,grp2 and p at the bottom of the graph (any table structure).

1
Which package are you using for pairwise_prop_test? It looks like a few packages might have functions with that name.Jon Spring
@Jon Spring: library (rstatix)Ged

1 Answers

1
votes

Here's an approach using dplyr to add row numbers and use those for vertical spacing.

library(dplyr)
ggplot (df, aes(x=grp2, fill=grp1)) + 
  geom_bar(position="fill") +
  geom_segment(data = ppt %>% dplyr::mutate(row = row_number() / n() * 0.5),
               aes(x = group1, xend = group2,
                   y = row, yend = row), inherit.aes = FALSE) +
  geom_text(data = ppt %>% mutate(row = row_number() / n() * 0.5),
            aes(x = group1, label = p, y = row), 
            inherit.aes = FALSE, vjust = -0.2, hjust = 0) 

enter image description here