不平衡类的ggplot2和hist()输出之间的不一致

时间:2015-07-24 16:46:19

标签: r ggplot2 histogram

我在区间[0,1](概率)中有两个正数向量。这两个向量用于两个不同的类别“正”和“负”。但正向量的大小远小于负向量的大小。

> str(pos)
 Named num [1:4269] 0.38 0.641 0.45 0.644 0.62 ...
 - attr(*, "names")= chr [1:4269] "486" "6516" "11901" "13564" ...

> str(neg)
 Named num [1:455634] 0.645 0.536 0.365 0.523 0.587 ...
 - attr(*, "names")= chr [1:455634] "31" "172" "174" "277" ...

现在,当我使用pos绘制两个向量neghist的直方图时,我得到以下结果:

hist(pos, xlab  = "pos", breaks = seq(0, 0.9, by = 0.004), col = "red")
hist(neg, xlab = "neg", breaks = seq(0, 0.9, by = 0.004),add = T, col=rgb(1, 0, .5, 0.5))

histogram by above code

显然,正面(暗红色)在数字上以负(粉红色)为主,这是完全有道理的。

但是当我在ggplot中做同样的事情时,如下所示,

library(ggplot2)
pos.frame <- data.frame(probability = pos)
neg.frame <- data.frame(probability = neg)

pos.frame$group <- "Positive"
neg.frame$group <- "Unknown/Negative"

all.frame <- rbind(pos.frame, neg.frame)

ggplot(all.frame, aes(probability, fill = group)) + geom_density(alpha = 0.2) +
  xlab("Probability of Being Interested in Fashion") + ylab("Density(In Thousands)")

我得到以下情节 ggplot version

显然歪曲了消极和积极的数量。

有人能说出我在做什么错了吗?

编辑:正如@Roland所建议,我做了以下事情:

ggplot(all.completeTest.frame, aes(probability, fill = group)) + geom_histogram(alpha = 0.2) + xlab("Probability of Being Interested in Fashion") + ylab("Density(In Thousands)")

得到这个: enter image description here

现在无法通过积极展示模式。有没有办法制作直方图,以便它也正确地捕获/显示模式?

0 个答案:

没有答案