1

我有离散数据,例如,我在范围内呈现

         Marks Freq cumFreq 
1  (37.9,43.1]    4       4    
2  (43.1,48.2]   16      20   
3  (48.2,53.3]   76      96    

我需要为这些数据绘制 cmf,我知道有

plot(ecdf(x))

但我不添加什么来获得我需要的东西。

4

2 回答 2

4

这里有几个选项:

library(ggplot2)
library(scales)
library(dplyr)

## Fake data
set.seed(2)
dat = data.frame(score=c(rnorm(130,40,10), rnorm(130,80,5)))

如果您有原始数据,以下是绘制 ECDF 的方法:

# Base graphics
plot(ecdf(dat$score))

# ggplot2
ggplot(dat, aes(score)) +
  stat_ecdf(aes(group=1), geom="step")

如果您只有汇总数据,这是绘制 ECDF 的一种方法:

首先,让我们将数据分组到 bin 中,类似于您在问题中的内容。我们使用该cut函数来创建 bin,然后创建一个新pct列来计算每个 bin 占总分数的分数。我们使用dplyr链接运算符 ( %>%) 在一个函数“链”中完成所有操作。

dat.binned = dat %>% count(Marks=cut(score,seq(0,100,5))) %>%
         mutate(pct = n/sum(n))

现在我们可以绘制它了。cumsum(pct)计算累积百分比(就像cumFreq你的问题一样)。geom_step使用这些累积百分比创建阶梯图。

ggplot(dat.binned, aes(Marks, cumsum(pct))) +
  geom_step(aes(group=1)) +
  scale_y_continuous(labels=percent_format()) 

这是情节的样子:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

于 2016-06-03T22:28:24.380 回答
0

那这个呢:

library(ggplot2)
library(scales)
library(dplyr)

set.seed(2)
dat = data.frame(score = c(rnorm(130,40,10), rnorm(130,80,5)))
dat.binned = dat %>% count(Marks = cut(score,seq(0,100,5))) %>%
         mutate(pct = n/sum(n))
ggplot(data = dat.binned, mapping = aes(Marks, cumsum(pct))) +  
  geom_line(aes(group = 1)) + 
  geom_point(data = dat.binned, size = 0.1, color = "blue") +
  labs(x = "Frequency(Hz)", y = "Axis") +
  scale_y_continuous(labels = percent_format()) 

在此处输入图像描述

于 2020-06-18T06:16:33.257 回答