0
votes

Trying to create a stacked bar plot with depth (in meters) on the y-axis and different categories on the x-axis and then filled with abundance values. However, ggplot2 keeps summing all the depth values (a+b+c) so that the y-axis isn't correct.

I know I need to specify geom_bar(stat = "identity"), but this seems to mess with the y-axis since all of the groups have the same values, but differing abundances.

##Data: 
Phylum  Depth   Abundance
Cnidaria    110 6
Cnidaria    90  12
Cnidaria    70  1
Cnidaria    50  4
Cnidaria    30  3
Cnidaria    20  1
Cnidaria    120 13
Cnidaria    80  3
Cnidaria    60  12
Arthropada  110 105
Arthropada  90  493
Arthropada  70  23
Arthropada  50  3
Arthropada  30  10
Arthropada  20  42
Arthropada  120 57
Arthropada  80  3
Arthropada  60  7

##Current plot: 
ggplot(data = data, aes(x = Phylum, y = Depth, fill = Abundance)) +
  geom_bar(stat = "identity")

The current plot outputs the data with the y-axis summed to 600 m when the highest value should only be 120 m.

1
Welcome to SO! When you place a question try to add a minimum content: input sample, expected output sample, what did you try, research and where are you stuck. What did you try? Not showing any effort...David García Bodego
Based on your description, stacking bars by Phylum category isn't what you want. Are you able to include a sketch of what you expect the result to be?Z.Lin

1 Answers

0
votes

Good thing is I do not think it is you code that is giving you trouble. Instead, I think you may want to consider an alternative way of displaying your data.

data = data.frame("Phylum" = c("Cnidaria", "Cnidaria", "Cnidaria", "Cnidaria","Cnidaria", "Cnidaria", "Cnidaria", "Cnidaria", "Cnidaria", "Arthropada", "Arthropada", "Arthropada", "Arthropada", "Arthropada", "Arthropada", "Arthropada", "Arthropada", "Arthropada"), "Depth" = c(110, 90, 70, 50, 30, 21, 120, 80, 60, 110, 90, 70, 50, 30, 20, 120, 80, 60), "Abundance" = c(6, 12, 1, 4, 3, 1, 13, 3, 12, 105, 493, 23, 3, 10, 42, 57, 3, 7))

The reason why it looks like the y-axis is adjusted is because the values are basically the same, which is shown by the averages of the categories:

aggregate(.~Phylum, data, mean)

      Phylum    Depth Abundance
1 Arthropada 70.00000 82.555556
2   Cnidaria 70.11111  6.111111

When you switch Depth and Abundance you get a plot more similar to what you are expecting:

p = ggplot(data = data, aes(x = Phylum, y = Abundance, fill = Depth))
p+geom_bar(stat = "identity")

You may want to consider different plots. For example, you may get better information by using Abundance and Depth for your x or y-axis and then filling in with the Phylum category.