Plotting with facet_wrap gives the error must be size 2 or 1, not 28

66 views Asked by At

I have a df like this:

Sample_ID group_classification Days_after_infection_1 category post-infection or 1st dose p.i. category# category post-1st dose post 1st dose category# category post-2nd dose post 2nd dose category# abs titers control_classification
120027_1 Control NA 0 0 NA NA NA NA Fractalkine_pg_mL_LOD(100) 5.502690e+03 control_positive
120027_1 Control NA 0 0 NA NA NA NA GM_CSF_pg_mL_LOD(0.12) 0.000000e+00 control_positive 
120027_1 Control NA 0 0 NA NA NA NA ITAC_pg_mL_LOD(1.5) 6.178953e+00 control_positive

The colnames():

[1]"Sample_ID"                           "group_classification"               
[3] "Days_after_infection_1"              "category post-infection or 1st dose"
[5] "p.i. category#"                      "category post-1st dose"             
[7] "post 1st dose category#"             "category post-2nd dose"             
[9] "post 2nd dose category#"             "abs"                                
[11] "titers"                              "control_classification"      

I have a line of code to perform a dunn_test:

df.dunn.stat <- data.frame(df.tidy.3) %>% 
  # group_by(`category post-1st dose`) %>%
  group_by(abs) %>%
  # filter(abs == abs[i]) %>%
  dunn_test(titers ~ group_classification) %>% 
  add_y_position(scales = 'free_y')

This gives me the following df:

 A tibble: 6 × 12
  abs             .y.   group1 group2    n1    n2 statistic      p p.adj p.adj.signif y.position groups
  <chr>           <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl> <dbl> <chr>             <dbl> <name>
1 Fractalkine_pg… tite… Contr… Induc…    10    14    -1.91  0.0559 0.329 ns               10085. <chr> 
2 Fractalkine_pg… tite… Contr… Natur…    10    38    -1.92  0.0548 0.329 ns               10333. <chr> 
3 Fractalkine_pg… tite… Contr… Hybrid    10    27    -1.71  0.0877 0.351 ns               10580. <chr> 
4 Fractalkine_pg… tite… Induc… Natur…    14    38     0.349 0.727  1     ns               10828. <chr> 
5 Fractalkine_pg… tite… Induc… Hybrid    14    27     0.484 0.628  1     ns               11075. <chr> 
6 Fractalkine_pg… tite… Natur… Hybrid    38    27     0.201 0.841  1     ns               11323. <chr> 

If you check the y.position column, the values are too big, and if I compare these values with my main df points, is even worse. So I performed the following, I mutated those values to have standard values that fix with my main data:

df.dunn.stat1 <- df.dunn.stat %>%
  group_by(abs) %>%
  mutate(y.position = c(7, 7.5, 8, 8.5, 9, 9.5))

Now the y.position column has values that are fixed in my plot:

# A tibble: 6 × 12
# Groups:   abs [1]
  abs             .y.   group1 group2    n1    n2 statistic      p p.adj p.adj.signif y.position groups
  <chr>           <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl> <dbl> <chr>             <dbl> <name>
1 Fractalkine_pg… tite… Contr… Induc…    10    14    -1.91  0.0559 0.329 ns                  7   <chr> 
2 Fractalkine_pg… tite… Contr… Natur…    10    38    -1.92  0.0548 0.329 ns                  7.5 <chr> 
3 Fractalkine_pg… tite… Contr… Hybrid    10    27    -1.71  0.0877 0.351 ns                  8   <chr> 
4 Fractalkine_pg… tite… Induc… Natur…    14    38     0.349 0.727  1     ns                  8.5 <chr> 
5 Fractalkine_pg… tite… Induc… Hybrid    14    27     0.484 0.628  1     ns                  9   <chr> 
6 Fractalkine_pg… tite… Natur… Hybrid    38    27     0.201 0.841  1     ns                  9.5 <chr> 

Here is the issue, I want to plot a facet_wrap, so I use the following code:

df.tidy.3 %>%
  ggplot(aes(x = group_classification, 
             y = titers)) +
  facet_wrap(~abs) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(data = natural.hybrid, 
              shape = 21, 
              width = 0.1, 
              size = 2,
              aes(fill = `category post-infection or 1st dose`))+
  geom_jitter(data = induced, 
              width = 0.1, 
              aes(color = `category post-1st dose`), 
              size = 2) +
  geom_jitter(data = control.df, width = 0.1,
              aes(color = 'Control_classification'), 
              size = 2) +
  scale_fill_viridis_d() +
  scale_color_manual(values = c(RColorBrewer::brewer.pal(3, 'Dark2')[c(1,3)], 
                                "black")) +
  # scale_y_continuous(trans = 'log10') +
  labs(x = '', 
       y = 'Cytokines Concentration') +
  stat_pvalue_manual(df.dunn.stat, 
                     step.increase = 0.1, 
                     hide.ns = 'p',
                     label = 'p.adj.signif',
                     # y.position = 20,
                     # position = position_dodge(0.8),
  )

In the line stat_pvalue_manual(df.dunn.stat,... is the key. If I plot the data with my old df.dunn.stat there is no problem, the plot is done, however, with visual issues. Now, if I put the df.dunn.stat1 I got the following error:

Error in `dplyr::mutate()`:
ℹ In argument: `label = as.character(data %>% pull("p.adj.signif"))`.
ℹ In group 1: `abs = "GM_CSF_pg_mL_LOD(0.12)"`.
Caused by error:
! `label` must be size 2 or 1, not 28.

To be honest I don't understand why this happens because the numbers 2 or 1 and 28 don't make sense to me, because my data or the unique values from abs columns don't have those numbers.

What can I do to fix this problem?

Do I need to perform another modification to plot together?

1

There are 1 answers

4
YBS On BEST ANSWER

Your error can be replicated with following code:

df1 <- mtcars %>% 
  group_by(vs) %>% 
  dplyr::mutate(cyl = c(5,10))

output1:

To eliminate the issue, try this

df.dunn.stat1 <- df.dunn.stat %>%
  dplyr::mutate(y.position = (y.position - 6500)/500)