累积频率直方图

时间:2019-08-06 05:56:22

标签: r

我希望我的“相对相对频率图”看起来像里面有一个直方图。对于我的代码,请参见下文:

max.data=max(week_final2$cured)
min.data=min(week_final2$cured)

slice=round((max.data-min.data)/25)
breaks=seq(min.data, max.data, by=slice)

cut.data=cut(week_final2$cured, breaks, right=FALSE)
cut.data

frequency=table(cut.data)
frequency

cummul.freq=cumsum(frequency)
cummul.freq

relative.frequency=frequency/sum(frequency)

cf=as.data.frame(cummul.freq)
cf
cummul.freq=cf[,1]
cummul.freq

cummul.percentile=cummul.freq/max(cummul.freq)
Cured_Freq <- cbind(frequency,relative.frequency,cummul.freq, cummul.percentile)

graph.cummul.perc =c(0, cummul.percentile)

plot(breaks, graph.cummul.perc, ylab="Relative Cumulative Frequency", 
     main="Weeks that Children are Cured(CURED)")
lines(breaks, graph.cummul.perc)

1 个答案:

答案 0 :(得分:0)

默认直方图功能包含您要查找的某些信息。如果您在不进行绘图的情况下进行存储,则可以执行以下操作:

x <- rnorm(1000)
breaks <- 30

distPlot <- function(x,breaks=25){
  require(ggplot2)
  require(dplyr)

  h <- hist(x,breaks = breaks,freq = F,plot = F)

  as_tibble(h[c('mids','density')]) %>%
    mutate(cdist=cumsum(density),rfreq=density/max(density)) %>% 
    mutate(cdist = cdist/max(cdist))%>%
    ggplot()+
    geom_col(aes(x=mids,y=rfreq))+
    geom_line(aes(x=mids,y=cdist))+
    xlab('x')+
    ylab('Relative frequency')
}

distPlot(x=x,breaks = breaks)
#> Loading required package: ggplot2
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Warning in hist.default(x, breaks = breaks, freq = F, plot = F): argument
#> 'freq' is not made use of

reprex package(v0.3.0)于2019-08-06创建