A minimal sub-set of my data is
mydata <- read.table(header = TRUE, text= "
Product Characteristic Product_category
AA Functional A
AB Functional A
AB Portable A
BA Portable B
BA Quality B
BB Quality B
BA Bright B
BB Sound B
BB Sound B
BC Sound B
BC Sound B
BC Work B
CA Functional C
CA Functional C
CA Functional C
CA Functional C
CB Functional C
CC Functional C
CC Functional C
CC Functional C
CC Functional C
CC Portable C
CC Design C
CD Quality C
CD Quality C
CD Output C
CD Noise C
CD Noise C
CD Component C
CD Component C
")
I want to make 3 barplots corresponding to each of the 3 product categories with x=Characteristic and y axis having count of each Characteristic. Further I want to stack the bar with Product. So the code for the barplot for lets say Product_category A is -
mydata %>% filter(Product_category == "A") %>%
ggplot(aes(x=Characteristic, fill = Product)) + geom_bar(width = 0.2) + coord_flip()
This part is easy. I am struggling with two things - I want to order the stacked bars in descending order of the count of each Characteristic. This dataset being the minimal subset of my dataset, so here by default the bars may appear ordered but in my actual dataset they are not. The second thing I want to do is to label each bar with percentage, such that the percentage is within each product category - formula = count(Characteristic)/sum(count(Characteristic)). So I want my final graph to look something as follows -
mydata %>% filter(Product_category == "A") %>%
group_by(Characteristic) %>%
summarize(counts = n()) %>% arrange(counts) %>%
mutate(Characteristic = factor(Characteristic, Characteristic), perc = counts/sum(counts)) %>%
ggplot(aes(x=Characteristic, y = counts)) +
geom_bar(stat = "identity", width = 0.4) +
theme(axis.text.x=element_blank()) +
geom_text(aes(label = paste(round(perc*100, digits = 1),"%",sep = "")), hjust = -0.2, size = 2.8, position = position_dodge(width = 0.7), inherit.aes = TRUE) +
coord_flip()
With the only difference that I want each bar to be stacked by Product, so I can visually see the share of each Product within each Characteristic. I experimented with many many things but each is verbose and still do not lend desired result. What is the most tidy way of doing this?