Search code examples
rggplot2bar-chartstacked-chart

Stacked bar chart on ggplot2 presenting wrong data


SOLVED: the issue was that the classes were set as characters rather than factor/numeric

I am trying to make a stacked bar chart from a long format data frame which I've created. The bar chart does come up correctly format wise but the data is incorrect to what is in the data frame. Here is my data:

1       Macrolides          X3        2.85
2      Penicillins          X3        6.64
3   Cephalosporins          X3        1.70
4       Quinolones          X3        1.36
5    Tetracyclines          X3        1.19
6       Macrolides     Belgium        2.62
7      Penicillins     Belgium       16.28
8   Cephalosporins     Belgium        1.59
9       Quinolones     Belgium        2.69
10   Tetracyclines     Belgium        2.10
11      Macrolides    Bulgaria        2.49
12     Penicillins    Bulgaria        8.05
13  Cephalosporins    Bulgaria        2.32
14      Quinolones    Bulgaria        1.99
15   Tetracyclines    Bulgaria        1.71
16      Macrolides      Cyprus        2.86
17     Penicillins      Cyprus       14.43
18  Cephalosporins      Cyprus        5.41
19      Quinolones      Cyprus        4.10
20   Tetracyclines      Cyprus        3.27
21      Macrolides       Czech        3.25
22     Penicillins       Czech        7.60
23  Cephalosporins       Czech        1.62
24      Quinolones       Czech        1.22
25   Tetracyclines       Czech        2.27
26      Macrolides     Denmark        2.34
27     Penicillins     Denmark       10.29
28  Cephalosporins     Denmark        0.03
29      Quinolones     Denmark        0.51
30   Tetracyclines     Denmark        1.70
31      Macrolides     Estonia        2.06
32     Penicillins     Estonia        4.22
33  Cephalosporins     Estonia        0.88
34      Quinolones     Estonia        0.81
35   Tetracyclines     Estonia        1.89
36      Macrolides     Finland        1.23
37     Penicillins     Finland        6.61
38  Cephalosporins     Finland        2.33
39      Quinolones     Finland        0.88
40   Tetracyclines     Finland        4.09
41      Macrolides      France        2.63
42     Penicillins      France       15.62
43  Cephalosporins      France        2.67
44      Quinolones      France        2.00
45   Tetracyclines      France        3.16
46      Macrolides     Germany        2.09
47     Penicillins     Germany        4.10
48  Cephalosporins     Germany        2.61
49      Quinolones     Germany        1.51
50   Tetracyclines     Germany        2.74
51      Macrolides      Greece        8.55
52     Penicillins      Greece       12.87
53  Cephalosporins      Greece        8.91
54      Quinolones      Greece        2.89
55   Tetracyclines      Greece        2.31
56      Macrolides     Hungary        2.42
57     Penicillins     Hungary        6.69
58  Cephalosporins     Hungary        1.92
59      Quinolones     Hungary        1.97
60   Tetracyclines     Hungary        1.38
61      Macrolides     Iceland        1.40
62     Penicillins     Iceland       12.08
63  Cephalosporins     Iceland        0.62
64      Quinolones     Iceland        1.02
65   Tetracyclines     Iceland        5.05
66      Macrolides     Ireland        3.63
67     Penicillins     Ireland       10.65
68  Cephalosporins     Ireland        1.21
69      Quinolones     Ireland        0.91
70   Tetracyclines     Ireland        2.60
71      Macrolides       Italy        4.98
72     Penicillins       Italy       14.55
73  Cephalosporins       Italy        2.55
74      Quinolones       Italy        3.45
75   Tetracyclines       Italy        0.53
76      Macrolides      Lativa        1.03
77     Penicillins      Lativa        5.59
78  Cephalosporins      Lativa        0.57
79      Quinolones      Lativa        0.91
80   Tetracyclines      Lativa        2.38
81      Macrolides   Lithuania        1.44
82     Penicillins   Lithuania        6.99
83  Cephalosporins   Lithuania        0.80
84      Quinolones   Lithuania        0.83
85   Tetracyclines   Lithuania        1.38
86      Macrolides  Luxembourg        3.46
87     Penicillins  Luxembourg       13.96
88  Cephalosporins  Luxembourg        4.18
89      Quinolones  Luxembourg        2.94
90   Tetracyclines  Luxembourg        2.04
91      Macrolides       Malta        2.97
92     Penicillins       Malta        9.80
93  Cephalosporins       Malta        5.04
94      Quinolones       Malta        1.79
95   Tetracyclines       Malta        1.02
96      Macrolides Netherlands        1.31
97     Penicillins Netherlands        4.36
98  Cephalosporins Netherlands        0.04
99      Quinolones Netherlands        0.87
100  Tetracyclines Netherlands        2.66
101     Macrolides      Norway        1.55
102    Penicillins      Norway        6.77
103 Cephalosporins      Norway        0.12
104     Quinolones      Norway        0.54
105  Tetracyclines      Norway        2.77
106     Macrolides      Poland        2.89
107    Penicillins      Poland        9.43
108 Cephalosporins      Poland        2.44
109     Quinolones      Poland        1.23
110  Tetracyclines      Poland        2.10
111     Macrolides    Portugal        3.37
112    Penicillins    Portugal       12.08
113 Cephalosporins    Portugal        1.81
114     Quinolones    Portugal        2.97
115  Tetracyclines    Portugal        0.71
116     Macrolides    Slovenia        1.89
117    Penicillins    Slovenia        9.67
118 Cephalosporins    Slovenia        0.40
119     Quinolones    Slovenia        1.10
120  Tetracyclines    Slovenia        0.04
121     Macrolides       Spain        1.88
122    Penicillins       Spain       12.64
123 Cephalosporins       Spain        1.56
124     Quinolones       Spain        2.54
125  Tetracyclines       Spain        0.70
126     Macrolides      Sweden        0.43
127    Penicillins      Sweden        7.09
128 Cephalosporins      Sweden        0.20
129     Quinolones      Sweden        0.77
130  Tetracyclines      Sweden        3.33
131     Macrolides          UK        2.71
132    Penicillins          UK        8.60
133 Cephalosporins          UK        0.55
134     Quinolones          UK        0.46
135  Tetracyclines          UK        4.15

This is the code for how I made the bar chart and a picture of it. As you can see the proportions in the chart are incorrect, e.g. The values for Penicillin use is very high compared the rest of the antibiotics but this isn't shown on my chart. I'm not sure what I've done incorrectly.

barchart <- ggplot(data=chart.new, aes(x = Country, y = DailyDosage, fill = Group, group = Group)) +
  geom_bar(stat = "identity") +
  coord_flip() + 
  scale_fill_manual(values = cbPalette) +
  labs(title = "Antibiotic Consumption by Group ") +
  theme(legend.position = "top")

graph


Solution

  • Similar to @DaveGruenewald, I got the correct values using your code and data. I noticed you had theme(legend.position = "top") while you showed a plot with legend on the side. Is it the same code, for reference you can try what I have below.

    What I used from your table above:

    chart.new=dput(chart.new)
    structure(list(Group = structure(c(2L, 3L, 1L, 4L, 5L, 2L, 3L, 
    1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 
    4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 
    5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 
    2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 
    3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 
    1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 
    4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 
    5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L, 2L, 3L, 1L, 4L, 5L
    ), .Label = c("Cephalosporins", "Macrolides", "Penicillins", 
    "Quinolones", "Tetracyclines"), class = "factor"), Country = structure(c(27L, 
    27L, 27L, 27L, 27L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 
    6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
    9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 
    12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 
    14L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 
    17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 
    20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 
    22L, 22L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 25L, 
    25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L), .Label = c("Belgium", 
    "Bulgaria", "Cyprus", "Czech", "Denmark", "Estonia", "Finland", 
    "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", 
    "Italy", "Lativa", "Lithuania", "Luxembourg", "Malta", "Netherlands", 
    "Norway", "Poland", "Portugal", "Slovenia", "Spain", "Sweden", 
    "UK", "X3"), class = "factor"), DailyDosage = c(2.85, 6.64, 1.7, 
    1.36, 1.19, 2.62, 16.28, 1.59, 2.69, 2.1, 2.49, 8.05, 2.32, 1.99, 
    1.71, 2.86, 14.43, 5.41, 4.1, 3.27, 3.25, 7.6, 1.62, 1.22, 2.27, 
    2.34, 10.29, 0.03, 0.51, 1.7, 2.06, 4.22, 0.88, 0.81, 1.89, 1.23, 
    6.61, 2.33, 0.88, 4.09, 2.63, 15.62, 2.67, 2, 3.16, 2.09, 4.1, 
    2.61, 1.51, 2.74, 8.55, 12.87, 8.91, 2.89, 2.31, 2.42, 6.69, 
    1.92, 1.97, 1.38, 1.4, 12.08, 0.62, 1.02, 5.05, 3.63, 10.65, 
    1.21, 0.91, 2.6, 4.98, 14.55, 2.55, 3.45, 0.53, 1.03, 5.59, 0.57, 
    0.91, 2.38, 1.44, 6.99, 0.8, 0.83, 1.38, 3.46, 13.96, 4.18, 2.94, 
    2.04, 2.97, 9.8, 5.04, 1.79, 1.02, 1.31, 4.36, 0.04, 0.87, 2.66, 
    1.55, 6.77, 0.12, 0.54, 2.77, 2.89, 9.43, 2.44, 1.23, 2.1, 3.37, 
    12.08, 1.81, 2.97, 0.71, 1.89, 9.67, 0.4, 1.1, 0.04, 1.88, 12.64, 
    1.56, 2.54, 0.7, 0.43, 7.09, 0.2, 0.77, 3.33, 2.71, 8.6, 0.55, 
    0.46, 4.15)), class = "data.frame", row.names = c(NA, 135L))
    

    Checking chart.new:

    str(chart.new)
    'data.frame':   135 obs. of  3 variables:
     $ Group      : Factor w/ 5 levels "Cephalosporins",..: 2 3 1 4 5 2 3 1 4 5 ...
     $ Country    : Factor w/ 27 levels "Belgium","Bulgaria",..: 27 27 27 27 27 1 1 1 1 1 ...
     $ DailyDosage: num  2.85 6.64 1.7 1.36 1.19 ...
    

    Using the default colors:

    ggplot(data=chart.new ,aes(x = Country, y = DailyDosage, fill = Group, group = Group))+
    geom_bar(stat = "identity")+ coord_flip() + 
    labs(title = "Antibiotic Consumption by Group ")+
     theme(legend.position = "top")
    

    enter image description here