0

I have

enter image description here

As you can see, all studies represented by nystudie, are printed on the x-axis. This creates a lot of empty groups, which I need help to remove.

> head(p)
  study response treatment
1    13        1       SSA
2    12        4       SSA
3    10        4       SSA
4     4        4      SSTR
5     4        3      SSTR
6     9        4       SSA

Each p$study belongs in either SSTR or SSA. I want to count p$response per p$study and then bind_rows to count all response per p$treatment.

I have

 p %>%
      mutate(nystudie=as.character(study),
             best.resp =as.factor(response)) %>%    
      bind_rows(., mutate(., nystudie="All")) %>%   
      group_by(nystudie,best.resp) %>%   
      summarise(N=n(),Val=unique(treatment))

Which gives

# A tibble: 6 x 4
# Groups:   nystudie, best.resp [6]
  nystudie best.resp     N Val  
  <chr>    <fct>     <int> <fct>
1 1        3             1 SSTR 
2 1        4             2 SSTR 
3 10       4             1 SSA  
4 11       4             2 SSA  
5 12       3             9 SSA  
6 12       4             4 SSA 

So, to stratify for p$treatmet, I wrote:

 %>% 
    ggplot(aes(nystudie, N, color = best.resp, fill= best.resp)) +
      geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
      facet_wrap(~Val,ncol = 2)

But, this create "empty groups". E.g. study 11, 12, 13, 14, 15 in SSTR and study 2, 22, 3, 4, 5, 6, 7 in SSA.

How can these "empty" groups be omitted in each facet_wrap, so it only contain studies which in fact applied that p$treatment?

p <- structure(list(study = structure(c(12L, 2L, 12L, 12L, 9L, 8L, 
13L, 2L, 12L, 15L, 1L, 13L, 2L, 12L, 9L, 16L, 8L, 3L, 5L, 13L, 
11L, 5L, 4L, 6L, 1L, 9L, 4L, 12L, 1L, 8L, 12L, 11L, 4L, 2L, 6L, 
3L, 12L, 4L, 5L, 8L, 12L, 12L, 5L, 12L, 4L, 13L, 12L, 10L, 4L, 
12L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", 
"10", "11", "12", "13", "14", "15", "22"), class = "factor"), 
    response = c("3", "3", "3", "4", "3", "1", "4", "3", "3", 
    "4", "4", "4", "3", "3", "2", "4", "1", "3", "3", "4", "4", 
    "2", "3", "3", "3", "2", "4", "3", "4", "1", "4", "4", "3", 
    "3", "4", "3", "3", "3", "2", "1", "4", "4", "3", "3", "4", 
    "4", "3", "4", "4", "3"), treatment = structure(c(2L, 1L, 
    2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 
    2L, 1L, 2L), .Label = c("SSTR", "SSA"), class = "factor")), row.names = c(NA, 
-50L), class = "data.frame")
cmirian
  • 2,572
  • 3
  • 19
  • 59

1 Answers1

1

Maybe this:

#Code
p %>%
  mutate(nystudie=as.character(study),
         best.resp =as.factor(response)) %>%    
  bind_rows(., mutate(., nystudie="All")) %>%   
  group_by(nystudie,best.resp) %>%   
  summarise(N=n(),Val=unique(treatment)) %>% 
  ggplot(aes(nystudie, N, color = best.resp, fill= best.resp)) +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
  facet_wrap(~Val,ncol = 2,scales='free')

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
  • 1
    @cmirian Yeah, also if interested check `facet_grid()` for a matrix style. It has other options like space and position of the strips :) – Duck Sep 27 '20 at 15:26
  • sorry to bother you again. When stratifying with `facet_wrap(~Val)`, it seems that "All" prints the sum for all/both treatments in both `facets` (you can see in your ggplot that `All` is identical). Is it possible for `All` printed in each `facet` to only print All counted within each `Val`? – cmirian Sep 27 '20 at 15:45
  • 1
    @cmirian Yes, it is possible try playing with the summarise function. I will do a check and tell you what is happening! – Duck Sep 27 '20 at 15:50
  • 1
    @cmirian Try first: `data <- p %>% mutate(nystudie=as.character(study), best.resp =as.factor(response)) %>% group_by(nystudie,best.resp) %>% summarise(N=n(),Val=unique(treatment)) %>% bind_rows( p %>% mutate(nystudie=as.character(study), best.resp =as.factor(response)) %>% group_by(best.resp,treatment) %>% summarise(N=n()) %>% mutate(nystudie="All") %>% rename(Val=treatment) ) ` – Duck Sep 27 '20 at 16:11
  • 1
    @cmirian And then `ggplot(data,aes(nystudie, N, color = best.resp, fill= best.resp)) + geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) + facet_wrap(~Val,ncol = 2,scales='free') ` – Duck Sep 27 '20 at 16:11