Stacked-Bar Chart from a group_by Tibble Not Stacking

时间:2019-04-16 22:35:07

标签: r ggplot2 bar-chart data-visualization

I am using R and have a tibble summarizing counts of people in apprenticeships by age and race ethnicity. People are categorized as being designated as of a minority race/ethnicity or white.

 RA3<- structure(list(Age = c(18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 
23, 24, 24, 25, 25, 26, 26, 27, 29, 29, 31, 33, 37, 39, 39, 42, 
47, 47, 49, 49, 50, 50, 52, 57, 57, 60, 66, 66), Minority = c("Minority", 
"White", "Minority", "White", "Minority", "White", "Minority", 
"White", "White", "Minority", "White", "Minority", "White", "Minority", 
"White", "Minority", "White", "Minority", "Minority", "White", 
"White", "White", "Minority", "Minority", "White", "Minority", 
"Minority", "White", "Minority", "White", "Minority", "White", 
"Minority", "Minority", "White", "White", "Minority", "White"
), n = c(3L, 1L, 4L, 3L, 5L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 3L, 
8L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 1L, 1L, 2L, 3L, 5L, 1L, 
3L, 1L, 1L, 2L, 1L, 2L, 1L, 3L, 1L)), row.names = c(NA, -38L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(Age = c(18, 
18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25, 26, 26, 
27, 29, 29, 31, 33, 37, 39, 39, 42, 47, 47, 49, 49, 50, 50, 52, 
57, 57, 60, 66, 66), Minority = c("Minority", "White", "Minority", 
"White", "Minority", "White", "Minority", "White", "White", "Minority", 
"White", "Minority", "White", "Minority", "White", "Minority", 
"White", "Minority", "Minority", "White", "White", "White", "Minority", 
"Minority", "White", "Minority", "Minority", "White", "Minority", 
"White", "Minority", "White", "Minority", "Minority", "White", 
"White", "Minority", "White"), .rows = list(1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
    31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L)), row.names = c(NA, 
-38L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

I'm trying to create a stacked bar chart in R using ggplotthat will show both Minority/White apprenticeship counts at each age category.

However when I run my code:

libraray(ggplot2)
BarChartRA <- ggplot(RA3, aes(x = Age, y =n, fill = Minority)) + 
  geom_bar(data = subset(RA3, Minority == "Minority"), stat = "identity", position = position_stack()) +
  geom_bar(data = subset(RA3, Minority == "White"), stat = "identity", position = position_stack()) +
  scale_fill_manual(values=c("purple","dark green"))+
  labs(x = "Age(Years)", y = "Number of OJ's", title = "Number of Registered Apprenticeships by Race/Ethnicity, Colorado, 2018")
BarChartRA

I don't appear to be getting a stacked chart.

enter image description here

As an example, at 18 years of age (farthest left) there should be 3 Minority apprenticeships and 1 White apprenticeship for a total of 4. But as you can see, the bar chart is only showing 3. The classes are overlapping each other.

I thought the position = "stack" statement in the geom_barstatement would resolve this.

What am I doing wrong?

1 个答案:

答案 0 :(得分:0)

Since you have already defined your mapping in your top layer ggplot(RA3, aes(x = Age, y =n, fill = Minority)), and your data is tidy in long format, you can simply do this

library(ggplot2)
df %>% ggplot(aes(x = Age, y = n, fill = Minority)) +
  geom_bar(stat = "identity",
           position = "stack") +
  scale_y_continuous(breaks = c(0:10))

As you can see, the fill aesthetic groups the data based on the Minority column and will stack the bars accordingly.

I believe what is happening on your plot, since you have specified the whole table then subset it twice and set the position = "stack" this has simply superimposed one bar over the other.