-1

I want my Cumulative Relative Frequency Graph looks like has a histogram inside. For my code, see below:

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 2
    Please provide a [minimal & reproducible example including sample data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It's not clear to me what you're trying to achieve. Posting code without sample data makes it difficult for others to reproduce your code attempt and understand your goal. – Maurits Evers Aug 06 '19 at 06:25

1 Answers1

0

The default histogram function has some of the information you're looking for. If you store it without plotting, you can do something like this:

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

Created on 2019-08-06 by the reprex package (v0.3.0)

Rohit
  • 1,967
  • 1
  • 12
  • 15