stat_bin2d,基于成功率填充

时间:2012-09-28 13:46:03

标签: r plot ggplot2 histogram

我有一张包含以下数据的表格:

> head(sweet)
  interval  urgency success
1     3138      761       1
2     3210     2189       1
3     3243     1256       1
4     8776      823       1
5     3094     1405       1
6     3137     1062       1

成功取值0和1.我正在寻找不同紧急值的成功率,所以我画了这样的直方图:

ggplot(sweet, aes(x=urgency, fill=success==0)) + geom_histogram(position='fill')

plot1

现在我想看一下紧急程度和间隔组合的成功率,但类似的方法没有帮助:

ggplot(sweet, aes(x=urgency, y=interval, fill=success==0)) + geom_bin2d()

plot2

有没有办法让填充连续显示成功/失败的比例而不是2d bin图上无用的二进制值?

1 个答案:

答案 0 :(得分:4)

您可以使用stat_summary2d

ggplot(sweet, aes(interval, urgency, z = success)) + stat_summary2d()

enter image description here