hk2
hk2

Reputation: 487

Plot grouped bar plot for each subcategory within top 2 categories using ggplot2

I have a dataset very similar to sample dataset given below:

| Category | SubCategory | id | State | Population |
|----------|-------------|----|-------|------------|
| A        | chair       | 1  | CA    | 100        |
| A        | desk        | 2  | CA    | 150        |
| A        | chair       | 3  | NY    | 80         |
| E        | fan         | 4  | AZ    | 60         |
| E        | ac          | 5  | PA    | 55         |
| B        | chair       | 6  | PA    | 78         |
| E        | fan         | 7  | PA    | 90         |
| B        | table       | 8  | PA    | 120        |
| A        | bed         | 9  | UT    | 105        |
| A        | chair       | 10 | NY    | 156        |
| A        | desk        | 11 | NY    | 99         |

Below is the dataframe:

category= c("A","A","A","E","E","B","E","B","A","A","A")
subcategory = c("chair","desk","chair","fan","ac","chair","fan","table","bed","chair","desk")
id = c(1,2,3,4,5,6,7,8,9,10,11)
population = c(100,150,80,60,55,78,90,120,105,156,99)
df= data.frame(category, subcategory, id, population, stringsAsFactors = T)

If we look at the table, the total count of column 'id' for each category A, B and E are 6,2,3 (Basically, the occurrence of each category). Now, I have been wanting to plot a grouped bar plot using ggplot2 for each subcategory belonging to top 2 category (Top 2 is determined by the count of column 'id'). So, my output should show only categories A and E and the count of column id should be plotted against y axis belonging to each subcategory (As shown below): enter image description here

My block of code is giving me top 2 subcategories based on count of id for each category, whereas, I'm looking to plot all the subcategories based on count of id for top 2 categories.

Upvotes: 0

Views: 784

Answers (1)

AnilGoyal
AnilGoyal

Reputation: 26218


df %>% add_count(category, name = 'rank') %>%
  filter(dense_rank(desc(rank)) %in% 1:2) %>%
  count(category, subcategory) %>%
  ggplot(aes(x = category, y = n, fill = subcategory)) +
  geom_col(position = 'dodge') +
  geom_text(aes(label = n), position = position_dodge(1), vjust = 1)

enter image description here

Upvotes: 1

Related Questions