Peran Bray
Peran Bray

Reputation: 39

stacked histogram with nested x axis in ggplot2

I'm trying to create a stacked histogram with a nested x axis

enter image description here

I've tried with ggplot but so far haven't come close

As can be seen from the image above, I'm trying to make a histogram that both show a total count per bin per year and the individual counts that make up the total count to be shown inside the bar

The individual counts that make the total are derived from the famtotal column which are related to the FAMILY_DESC column

To sum it up I've been trying to make cross between a histogram and a stacked bar chart

The next part is to have a nested x axis so there would be 6 bins and that would be repeated for all the years

I hope this made sense and thank you very much in advance

structure(list(Year = c("2009", "2009", "2009", "2009", "2009", 
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009", 
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009", 
"2009", "2009", "2009", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2015", "2015", "2015", "2015", "2015", 
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015", 
"2015", "2015", "2015"), FAMILY_DESC = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L, 11L, 
11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 
5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 
12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 
1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L, 
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("ACROPORIDAE", 
"AGARICIIDAE", "DENDROPHYLLIIDAE", "EUPHYLLIDAE", "FAVIIDAE", 
"FUNGIIDAE", "MERULINIDAE", "MUSSIDAE", "OCULINIDAE", "PECTINIIDAE", 
"POCILLOPORIDAE", "PORITIDAE", "SIDERASTREIDAE", "UNKNOWN"), class = "factor"), 
    bin = c("five", "four", "one", "three", "two", "zero", "five", 
    "four", "one", "three", "two", "zero", "five", "four", "one", 
    "three", "two", "zero", "five", "four", "one", "three", "two", 
    "zero", "five", "four", "one", "three", "two", "zero", "five", 
    "four", "one", "three", "two", "zero", "five", "four", "one", 
    "three", "two", "zero", "five", "four", "one", "three", "two", 
    "zero", "five", "four", "one", "three", "two", "zero", "five", 
    "four", "one", "three", "two", "zero", "five", "four", "one", 
    "three", "two", "zero", "five", "four", "one", "three", "two", 
    "zero", "five", "four", "one", "three", "two", "zero", "five", 
    "four", "one", "three", "two", "zero", "five", "four", "one", 
    "three", "two", "zero", "five", "four", "one", "three", "two", 
    "zero"), famtotal = c(0L, 1L, 87L, 1L, 4L, 136L, 0L, 1L, 
    35L, 2L, 7L, 6L, 0L, 1L, 33L, 8L, 12L, 64L, 0L, 1L, 37L, 
    1L, 8L, 20L, 0L, 2L, 88L, 3L, 6L, 122L, 0L, 2L, 57L, 4L, 
    9L, 16L, 0L, 5L, 43L, 4L, 15L, 81L, 0L, 1L, 32L, 1L, 7L, 
    25L, 0L, 1L, 57L, 4L, 9L, 100L, 0L, 6L, 38L, 7L, 9L, 8L, 
    0L, 0L, 29L, 8L, 8L, 47L, 0L, 0L, 26L, 2L, 4L, 23L, 0L, 0L, 
    65L, 1L, 14L, 124L, 0L, 2L, 29L, 1L, 3L, 19L, 0L, 5L, 38L, 
    4L, 5L, 50L, 0L, 0L, 20L, 0L, 6L, 18L), yeartotal = c(0L, 
    4L, 192L, 12L, 31L, 226L, 0L, 4L, 192L, 12L, 31L, 226L, 0L, 
    4L, 192L, 12L, 31L, 226L, 0L, 4L, 192L, 12L, 31L, 226L, 0L, 
    10L, 220L, 12L, 37L, 244L, 0L, 10L, 220L, 12L, 37L, 244L, 
    0L, 10L, 220L, 12L, 37L, 244L, 0L, 10L, 220L, 12L, 37L, 244L, 
    0L, 7L, 150L, 21L, 30L, 178L, 0L, 7L, 150L, 21L, 30L, 178L, 
    0L, 7L, 150L, 21L, 30L, 178L, 0L, 7L, 150L, 21L, 30L, 178L, 
    0L, 7L, 152L, 6L, 28L, 211L, 0L, 7L, 152L, 6L, 28L, 211L, 
    0L, 7L, 152L, 6L, 28L, 211L, 0L, 7L, 152L, 6L, 28L, 211L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -96L), .Names = c("Year", 
"FAMILY_DESC", "bin", "famtotal", "yeartotal"), vars = c("Year", 
"FAMILY_DESC"))

Upvotes: 3

Views: 744

Answers (2)

Roman
Roman

Reputation: 17648

You can try this. Similar to the other good answer but with some major changes to imitate your scheme.

ggplot(d, aes(x=factor(bin, levels = c("zero","one", "two", "three", "four", "five")), 
              y=yeartotal, fill=FAMILY_DESC)) + 
  geom_col() +
  xlab("") +
  facet_grid(~Year, switch = "x") +
  theme(panel.border = element_blank(),
        panel.spacing.x = unit(0,"cm"),
        strip.placement = "outside",
        strip.background = element_blank())

enter image description here

Upvotes: 3

MrGumble
MrGumble

Reputation: 5766

Where dat is your data.frame, and bin would probably need to be re-ordered:

ggplot(dat, aes(x=bin, y=yeartotal, fill=FAMILY_DESC)) + geom_col() +
  facet_wrap(~Year)

Although the heights do not quite correspond to your drawing.

Because you already have done the counting in bins, you do not want to use geom_bar, as it defaults to start counting. Instead we use geom_col.

Upvotes: 2

Related Questions