Search code examples
rcrosstab

Plot crosstable information


I have a dataframe likes:

    Gender   Like  
    male     yes    
    female   no  
    female   yes
    other    yes
    male     no
    male     no
    female   no  
    female   yes
    other    no
    male     no
    male     yes

Based on this dataframe, I would like to draw a histogram that specifies each gender and the number of 'yes'and 'no' per gender (see picture)

enter image description here

If I use table(likes), I get a table that specifies the number of yes' and no's per gender. However, if I use plot(table(likes)), I get a really strange plot that is hard to interpret.

What can I do, to get such output?


Solution

  • You can do something like this-

    ggplot(data = dt) +
      aes(x = Gender, fill = Like) +
      geom_bar(position = "dodge") +
      scale_fill_brewer(palette = "YlGnBu") +
      theme_minimal() +   
      geom_text(aes(label=..count..),stat='count',position=position_dodge(0.9))
    

    Output-

    enter image description here

    If you want Like as your labels then use this-

    ggplot(data = dt) +
      aes(x = Gender, fill = Like) +
      geom_bar(position = "dodge") +
      scale_fill_brewer(palette = "YlGnBu") +
      theme_minimal() +   
      geom_text(aes(label=paste(Like)),stat='count',position=position_dodge(0.9))
    

    Output 2-

    enter image description here