I would like to display the counts of a survey in a barplot, binned by country and stacked by gender.
What I have been able to do so far is to convert the answers into a table using the table() function and plotting these binned by country sorted according to frequency. However, I am unable to stack the counts by gender in a way that sorts the table according to the number of my observations per country.
I fail to create a MWE, so instead I will post the table as far as I have gotten so far:
           A    B    C      D     E
  Female   35   7    30     9    11
  Male     30   6     9     7     3
  Other     0   0     1     1     0
When I input this table into a barplot function, it won't sort the bar plot according to observations in each country (columns). When I use the sort function, it converts the table into a vector. The output I am hoping for looks as follows:
           A    B    C      D     E
  Female   35   30   9     11    7
  Male     30   9     7     3     6
  Other     0   0     1     0     1
So that ultimately the bar plot is ordered by the sum of the country counts and then by gender.
Other things I haved tried so far: Converting the table to a matrix and then using this tutorial here on how to sort matrices. Sorting the table this way, also converts it to a vector.
 
     
    
