I am trying to build a stacked bar chart classifying the number of audits in our company by type of vendor and trigger, that is two dependent variables y1 and y2. Type of vendor is the name of the vendor and trigger is internal, external or contractor. I know how to build a stacked bar chart for one dependent variable by sorting by cumulative percent, but it doesn't work for two variables. Here is my code:
library(ggplot2)
library(reshape2)
library(tidyverse)
library(scales)
library(dplyr)
Audits.noNAs = Audits[which((!(is.na(Audits$`Vendor Name`)))),]
Audits.noNAs %>%
count(`Vendor Name`) %>%
mutate(perc3 = n / nrow(Audits.noNAs)) -> VendorAudits2
nRec = nrow(Audits.noNAs)
vendor.audits.ordered =
ggplot(VendorAudits2, aes(x = reorder(Vendor,
-perc3),
y = as.integer(nRec*perc3))) +
geom_bar(aes(fill=`Vendor Name`),position = "dodge",
stat = "identity") + ylab("count") + ylim(0,40) +
scale_x_discrete(name = "Vendor Name") +
scale_y_discrete(name = "count",
limits = paste(seq(from=0,to=40,by=2))) +
theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1))
vendor.audits.ordered
Audits %>%
count(Trigger) %>%
mutate(perc2 = n / nrow(Audits)) -> TriggerAudits
nRecT = nrow(Audits)
trigger.audits.ordered =
ggplot(TriggerAudits, aes(x = reorder(Trigger,
-perc2),
y = as.integer(nRecT*perc2))) +
geom_bar(aes(fill=Trigger),position = "dodge",
stat = "identity") + ylab("count") + ylim(0,40) +
scale_x_discrete(name = "Trigger") +
scale_y_discrete(name = "count",
limits = paste(seq(from=0,to=40,by=2))) +
theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1))
trigger.audits.ordered
As you can see they have been sorted separately but I would like to combine both operations into two simultaneous ones. I would also like to be able to combine both types of data while maintaining the exclusion of NA values for vendors (since those don't have any vendor related) or maybe have a separate column for those without vendor.