Search code examples
rplotggplot2histogramdensity-plot

Plotting numeric frequency data based on midpoints of bins in base R or ggplot


I am replicating simulation results from a published study. The authors present their output in a density plot (below) that is very rectangular. They appear to have created bins and then plotted the midpoint of the bins. Is it possible to easily build this type of plot using base R graphics or ggplot?

enter image description here


Solution

  • Very simple indeed:

    data=rnorm(1000)
    hist_info=hist(data, plot=F)
    plot(hist_info$mids, hist_info$density, type="l", xlab="X", ylab="density")
    

    enter image description here