I would like to overlay a density function over a histogram and have a strong preference for using the AlgebraOfGraphics to graph this exact function. Currently, to accomplish this I have to translate the function into a dataframe of values and then, plot the dataframe. Might the data()
function somehow be able to take an interval and function as inputs.
This is the graph I want to create:
And here is some code where I want to skip the part of creating pdfDF
:
using Random, Distributions, DataFrames, CairoMakie, AlgebraOfGraphics
CairoMakie.activate!(type = "svg") # crisper plots than default png
betaSamplingDist = Beta(2,2) ## create RV
## get N samples
N = 1000
Random.seed!(123) # Setting the seed so we can get the same random numbers
dataDF = DataFrame(samples = rand(betaSamplingDist,N)) ##**
## samples easy to plot using AlgebraOfGraphics
kumaHist = data(dataDF) * mapping(:samples) *
visual(Hist, normalization = :pdf, bins = 30, color = :cadetblue)
draw(kumaHist, axis = (xlabel = "samples", ylabel = "y"))
## make dataframe for plotting
pdfDF = DataFrame(xVal = 0:0.001:1)
pdfDF."density" = pdf(betaSamplingDist,pdfDF.xVal)
## combine histogram of sample and pdf from density function
overlayDensityPlot = kumaHist +
data(pdfDF) * mapping(:xVal,:density) * visual(Lines, color = :orange, linewidth = 8)
draw(overlayDensityPlot, axis = (xlabel = "y", ylabel = "density(y)"))
## is there a way to draw the above without creating the intermediate dataframe?
## for example, here is the Makie way to plot a function without a DataFrame
plot(0..1, x -> pdf(betaSamplingDist,x), color = :orange, linewidth = 8,
axis = (xlabel = "y", ylabel = "density(x)"))