本文介绍了用ggplot绘制表对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

         No    Yes
Female  411   130
Male    435   124

使用标准表命令创建的

.现在,使用图可以这样绘制:

which was created using the standard table command. Now with plot I can plot this as such:

plot(table(df$gender, df$fraud))

,然后输出2x2条形图.

and it then outputs a 2x2 bar chart.

所以我的问题是,如何使用ggplot2做到这一点?有没有办法将表对象转换为数据框?我会这样做,但是这变得一团糟,然后您需要重命名列和行标题,而这实际上只是一件很简单的事情而变成一团糟?

So my question is, how can I do this with ggplot2? Is there any way with out transforming the table-object to a data frame? I would do that, but it becomes a mess and you then need to rename column and row headers and it just becomes a mess for what is really a quite simple thing?

推荐答案

诸如

ggplot(as.data.frame(table(df)), aes(x=gender, y = Freq, fill=fraud)) +
    geom_bar(stat="identity")

以最少的重新标记次数获得相似的图表.

gets a similar chart with a minimum amount of relabelling.

这篇关于用ggplot绘制表对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:38