Home > Software engineering >  Making a Stacked Bar Graph for character occurence
Making a Stacked Bar Graph for character occurence

Time:12-20

I am having trouble plotting a stacked bar graph for three columns where I wanted to try to have two sets of all the variations of df$S on the x axis (one for I15 and I19). Then, I want to fill with the frequency of each character in their respective columns. So essentially, three columns of "NONE", "SILENT", and "MISSENSE" for I15 and three columns for I19 and then fill them with how many times each character appears, if that makes sense. But whenever I try to plot it comes up empty with only the y and x axis labeled. I tried using a code Ive used in the past for stacked box graphs but no luck.

df %>%
    pivot_longer(-S) %>%
    ggplot(aes(x = S, y = value, fill = c(I15, I19))


              S  I15  I19
    1      NONE  1/1 <NA>
    2      NONE  0/1  1/0
    3      NONE  1/1  1/1
    4      NONE  0/1  0/1
    5      NONE  0/0 <NA>
    6      NONE  1/1  1/1
    7      NONE  1/1  1/1
    8      NONE  0/1  1/0
    9      NONE  0/1  1/0
    10     NONE  1/1  1/1
    11     NONE  0/1  0/1
    12     NONE  1/1  1/1
    13     NONE  0/1  1/0
    14     NONE  1/1  1/1
    15 MISSENSE  0/1  0/1
    16 MISSENSE  0/1  0/1
    17     NONE <NA> <NA>
    18     NONE  0/1  1/0
    19     NONE  1/1  1/1
    20     NONE  1/1  1/1
    21     NONE  1/1  1/1
    22     NONE  1/1  1/1
    23     NONE  1/1  1/1
    24     NONE <NA> <NA>
    25     NONE <NA> <NA>
    26     NONE <NA> <NA>
    27     NONE  0/1  1/1
    28     NONE  0/1  1/1
    29     NONE  0/1  1/1
    30     NONE  1/1  1/1
    31   SILENT  1/1 <NA>
    32     NONE  1/1  1/1
    33   SILENT  1/1  1/1
    34     NONE  0/1  0/1
    35     NONE  0/1  0/1
    36     NONE  0/1  1/0
    37 MISSENSE  0/1  1/0
    38     NONE  0/1  1/0
    39   SILENT  0/1  0/0
    40     NONE  1/1  1/1
    41     NONE  0/1  1/0
    42     NONE  1/1  1/0
    43     NONE  0/1  1/0
    44     NONE  0/1  1/0
    45     NONE  0/1  0/1
    46     NONE  0/1  0/1
    47     NONE  0/1  0/1
    48     NONE  0/1  0/1
    49     NONE <NA>  0/0
    50   SILENT  0/1  0/1

CodePudding user response:

Does this help? Count by group (S, name, value) and use facets for the names, to make the x-axis less crowded.

df %>% 
  pivot_longer(-S) %>% 
  count(S, name, value) %>% 
  ggplot(aes(S, n))   
  geom_col(aes(fill = value))   
  facet_grid(name ~ .)

Result:

enter image description here

  • Related