Stacked grouped bar chart with ggseg: uneven number of categories in some groups, visualise?

Hello,

I'm am working on a stacked grouped bar chart using ggplot. I have an uneven number of categories in my data in different groups, which gives problems when I am trying to plot.

This is my data, person (1-6 in combination with 00 or xx), variable (A,B,C), cat (a,b,c), and value

div10
person variable cat value
1 1.oo A a 92
2 1. xx A a 10
3 1.oo A b 7
4 1. xx A b 1
5 1. xx A c 17
6 1.oo B a 2
7 1. xx B a 5
8 1.oo B b 0
9 1. xx B b 0
10 1. xx B c 5
11 1.oo C a 19
12 1. xx C a 5
13 1.oo C b 1
14 1. xx C b 0
15 1. xx C c 6
16 2.oo A a 3
17 2. xx A a 0
18 2.oo A b 1
19 2. xx A b 0
20 2. xx A c 8
21 2.oo B a 0
22 2. xx B a 0
23 2.oo B b 0
24 2. xx B b 0
25 2. xx B c 4
26 2.oo C a 0
27 2. xx C a 0
28 2.oo C b 0
29 2. xx C b 0
30 2. xx C c 3
31 3.oo A a 2
32 3. xx A a 0
33 3.oo A b 0
34 3. xx A b 2
35 3. xx A c 10
36 3.oo B a 0
37 3. xx B a 0
38 3.oo B b 0
39 3. xx B b 0
40 3. xx B c 1
41 3.oo C a 1
42 3. xx C a 0
43 3.oo C b 0
44 3. xx C b 0
45 3. xx C c 6
46 4.oo A a 64
47 4. xx A a 2
48 4.oo A b 19
49 4. xx A b 3
50 4. xx A c 5
51 4.oo B a 16
52 4. xx B a 5
53 4.oo B b 7
54 4. xx B b 0
55 4. xx B c 2
56 4.oo C a 16
57 4. xx C a 2
58 4.oo C b 0
59 4. xx C b 2
60 4. xx C c 0
61 5.oo A a 10
62 5. xx A a 0
63 5.oo A b 0
64 5. xx A b 0
65 5. xx A c 0
66 5.oo B a 3
67 5. xx B a 0
68 5.oo B b 0
69 5. xx B b 0
70 5. xx B c 0
71 5.oo C a 1
72 5. xx C a 0
73 5.oo C b 0
74 5. xx C b 0
75 5. xx C c 0
76 6.oo A a 3
77 6. xx A a 0
78 6.oo A b 0
79 6. xx A b 1
80 6. xx A c 0
81 6.oo B a 13
82 6. xx B a 6
83 6.oo B b 5
84 6. xx B b 0
85 6. xx B c 4
86 6.oo C a 2
87 6. xx C a 3
88 6.oo C b 0
89 6. xx C b 0
90 6. xx C c 0

And this is my code and plot:

ggplot(div10, aes(x = cat, y = value, fill = variable)) + 
  geom_bar(width=1, stat = 'identity', colour="black",position = position_stack(reverse=TRUE)) + facet_grid(~ person)+
  xlab("Type") + 
  ylab("amount") +
  theme(axis.text.x = element_text(angle=90, vjust=0, hjust=1))+
  theme(legend.position="bottom", legend.title = element_blank())+
  scale_fill_manual(values = c("dimgrey", "deepskyblue", "blue"))+
  scale_y_continuous(breaks=c(0,20,40,60,80,100,120))+
  theme(panel.grid.major.x=element_blank(),panel.background= element_blank(), panel.spacing.x = unit(0,"lines"),panel.grid = element_line(size = 0.2, linetype = 'solid',colour = "grey"))

Rplot

"1.xx" and "1.oo" are different but related groups. Each number has a "xx" and "oo" group (so basically two groups). The "xx" groups always have cat 'c' but the "oo" group never have cat 'c' (which you can see in my data). However, for all the "oo", R still plots the cat 'c', and then just doesn't give any data (since it is not in the dataframe). Is it possible to get rid of the plotting of cat 'c' in the "oo" groups, but leave them in the "xx"groups? So that I would have 3 bars for each "xx"group, and 2 bars for each "oo" group?

Thank you very much in advance!
Ellen

Use facet_grid(~ person, scales = "free_x")

library(tidyverse)

div10 <- data.frame(
  stringsAsFactors = FALSE,
            person = c("1.oo","1.xx","1.oo","1.xx",
                       "1.xx","1.oo","1.xx","1.oo","1.xx","1.xx","1.oo",
                       "1.xx","1.oo","1.xx","1.xx","2.oo","2.xx","2.oo",
                       "2.xx","2.xx","2.oo","2.xx","2.oo","2.xx","2.xx",
                       "2.oo","2.xx","2.oo","2.xx","2.xx","3.oo","3.xx",
                       "3.oo","3.xx","3.xx","3.oo","3.xx","3.oo","3.xx",
                       "3.xx","3.oo","3.xx","3.oo","3.xx","3.xx","4.oo",
                       "4.xx","4.oo","4.xx","4.xx","4.oo","4.xx","4.oo","4.xx",
                       "4.xx","4.oo","4.xx","4.oo","4.xx","4.xx","5.oo",
                       "5.xx","5.oo","5.xx","5.xx","5.oo","5.xx","5.oo",
                       "5.xx","5.xx","5.oo","5.xx","5.oo","5.xx","5.xx",
                       "6.oo","6.xx","6.oo","6.xx","6.xx","6.oo","6.xx",
                       "6.oo","6.xx","6.xx","6.oo","6.xx","6.oo","6.xx",
                       "6.xx"),
          variable = c("A","A","A","A","A","B",
                       "B","B","B","B","C","C","C","C","C","A","A","A",
                       "A","A","B","B","B","B","B","C","C","C","C",
                       "C","A","A","A","A","A","B","B","B","B","B",
                       "C","C","C","C","C","A","A","A","A","A","B","B",
                       "B","B","B","C","C","C","C","C","A","A","A",
                       "A","A","B","B","B","B","B","C","C","C","C","C",
                       "A","A","A","A","A","B","B","B","B","B","C",
                       "C","C","C","C"),
               cat = c("a","a","b","b","c","a",
                       "a","b","b","c","a","a","b","b","c","a","a","b",
                       "b","c","a","a","b","b","c","a","a","b","b",
                       "c","a","a","b","b","c","a","a","b","b","c",
                       "a","a","b","b","c","a","a","b","b","c","a","a",
                       "b","b","c","a","a","b","b","c","a","a","b",
                       "b","c","a","a","b","b","c","a","a","b","b","c",
                       "a","a","b","b","c","a","a","b","b","c","a",
                       "a","b","b","c"),
             value = c(92,10,7,1,17,2,5,0,0,5,
                       19,5,1,0,6,3,0,1,0,8,0,0,0,0,4,0,0,0,
                       0,3,2,0,0,2,10,0,0,0,0,1,1,0,0,0,6,64,2,
                       19,3,5,16,5,7,0,2,16,2,0,2,0,10,0,0,0,
                       0,3,0,0,0,0,1,0,0,0,0,3,0,0,1,0,13,6,5,
                       0,4,2,3,0,0,0)
)

ggplot(div10, aes(x = cat, y = value, fill = variable)) + 
    geom_col(width = 1,
             colour = "black",
             position = position_stack(reverse = TRUE)) + 
    facet_grid(~ person, scales = "free_x") +
    xlab("Type") + 
    ylab("amount") +
    scale_fill_manual(values = c("dimgrey", "deepskyblue", "blue")) +
    scale_y_continuous(breaks = c(0,20,40,60,80,100,120)) + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
          legend.position="bottom", legend.title = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.background = element_blank(),
          panel.spacing.x = unit(0,"lines"),
          panel.grid = element_line(size = 0.2,
                                    linetype = 'solid',
                                    colour = "grey"))

Created on 2020-10-29 by the reprex package (v0.3.0.9001)

NOTE: Next time please share your data in a copy/paste friendly format as explained here

Thank you so much! The only thing which I notice now is that I would like to change the order of the Type a,b,c. in the graph (this makes sense with my real datanames) I did this by using "scale_x_discrete(limits=c("a", "c", "b"). However, by ading "scales = "free_x" this doesn't work any more. Do you know how I could fix this? And is it also possible to make all the bars the same width?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

library(tidyverse)

div10 <- data.frame(
    stringsAsFactors = FALSE,
    person = c("1.oo","1.xx","1.oo","1.xx",
               "1.xx","1.oo","1.xx","1.oo","1.xx","1.xx","1.oo",
               "1.xx","1.oo","1.xx","1.xx","2.oo","2.xx","2.oo",
               "2.xx","2.xx","2.oo","2.xx","2.oo","2.xx","2.xx",
               "2.oo","2.xx","2.oo","2.xx","2.xx","3.oo","3.xx",
               "3.oo","3.xx","3.xx","3.oo","3.xx","3.oo","3.xx",
               "3.xx","3.oo","3.xx","3.oo","3.xx","3.xx","4.oo",
               "4.xx","4.oo","4.xx","4.xx","4.oo","4.xx","4.oo","4.xx",
               "4.xx","4.oo","4.xx","4.oo","4.xx","4.xx","5.oo",
               "5.xx","5.oo","5.xx","5.xx","5.oo","5.xx","5.oo",
               "5.xx","5.xx","5.oo","5.xx","5.oo","5.xx","5.xx",
               "6.oo","6.xx","6.oo","6.xx","6.xx","6.oo","6.xx",
               "6.oo","6.xx","6.xx","6.oo","6.xx","6.oo","6.xx",
               "6.xx"),
    variable = c("A","A","A","A","A","B",
                 "B","B","B","B","C","C","C","C","C","A","A","A",
                 "A","A","B","B","B","B","B","C","C","C","C",
                 "C","A","A","A","A","A","B","B","B","B","B",
                 "C","C","C","C","C","A","A","A","A","A","B","B",
                 "B","B","B","C","C","C","C","C","A","A","A",
                 "A","A","B","B","B","B","B","C","C","C","C","C",
                 "A","A","A","A","A","B","B","B","B","B","C",
                 "C","C","C","C"),
    cat = c("a","a","b","b","c","a",
            "a","b","b","c","a","a","b","b","c","a","a","b",
            "b","c","a","a","b","b","c","a","a","b","b",
            "c","a","a","b","b","c","a","a","b","b","c",
            "a","a","b","b","c","a","a","b","b","c","a","a",
            "b","b","c","a","a","b","b","c","a","a","b",
            "b","c","a","a","b","b","c","a","a","b","b","c",
            "a","a","b","b","c","a","a","b","b","c","a",
            "a","b","b","c"),
    value = c(92,10,7,1,17,2,5,0,0,5,
              19,5,1,0,6,3,0,1,0,8,0,0,0,0,4,0,0,0,
              0,3,2,0,0,2,10,0,0,0,0,1,1,0,0,0,6,64,2,
              19,3,5,16,5,7,0,2,16,2,0,2,0,10,0,0,0,
              0,3,0,0,0,0,1,0,0,0,0,3,0,0,1,0,13,6,5,
              0,4,2,3,0,0,0)
)

div10 %>% 
    mutate(cat = factor(cat, levels = c("a", "c", "b"))) %>% 
    ggplot(aes(x = cat, y = value, fill = variable)) + 
    geom_col(width = 1,
             colour = "black",
             position = position_stack(reverse = TRUE)) + 
    facet_grid(~ person,
               scales = "free_x",space = "free_x") +
    xlab("Type") + 
    ylab("amount") +
    scale_fill_manual(values = c("dimgrey", "deepskyblue", "blue")) +
    scale_y_continuous(breaks = c(0,20,40,60,80,100,120)) + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
          legend.position="bottom", legend.title = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.background = element_blank(),
          panel.spacing.x = unit(0,"lines"),
          panel.grid = element_line(size = 0.2,
                                    linetype = 'solid',
                                    colour = "grey"))

Created on 2020-10-30 by the reprex package (v0.3.0.9001)

1 Like

Thank you so much!! This looks great!