绘制交叉表信息

Plot crosstable information

我有一个数据框 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

基于这个数据框,我想绘制一个直方图,指定每个性别和每个性别的“是”和 'no' 的数量(见图)

如果我使用 table(likes),我会得到一个 table,指定每个性别的是和否的数量。但是,如果我使用 plot(table(likes)),我会得到一个非常奇怪的难以解释的情节。

我该怎么做才能得到这样的输出?

你可以这样做-

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))

输出-

如果你想 Like 作为你的 labels 然后使用这个-

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))

输出 2-