# Graph function using AlgebraOfGraphics

**URL:** https://discourse.julialang.org/t/graph-function-using-algebraofgraphics/67888
**Category:** Visualization
**Tags:** makie, algebraofgraphics
**Created:** [September 8, 2021, 6:43pm UTC](https://discourse.julialang.org/t/graph-function-using-algebraofgraphics/67888 "2021-09-08T18:43:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)
#### Post date: [September 8, 2021, 6:43pm UTC](https://discourse.julialang.org/t/graph-function-using-algebraofgraphics/67888/1 "2021-09-08T18:43:12Z")

</div>

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:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/f/df7aa527f320925dcfc5bb02bb960c296a34c361.png)

And here is some code where I want to skip the part of creating `pdfDF`:

```julia
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)"))

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [September 15, 2021, 1:18pm UTC](https://discourse.julialang.org/t/graph-function-using-algebraofgraphics/67888/2 "2021-09-15T13:18:55Z")

</div>

For these cases (plots that don’t depend on any data, like a `pdf` of a given distribuion), I think you can just combine Makie and AlgebraOfGraphics, for example

```julia
fg = draw(kumaHist , axis = (xlabel = "y", ylabel = "density(y)"))
ax = only(fg.grid).axis # `fg.grid` here has only one element as there is no layout
plot!(ax, 0..1, x -> pdf(betaSamplingDist,x), color = :orange, linewidth = 8)
# maybe `plot!(ax, betaSamplingDist)` already works and figures out the extrema

```

But I see your point, ideally one should allow things like

```julia
data((x=0..1, y=t -> pdf(dist, t)) * mapping(:x, :y)

```

I’ll try to think if there is an easy way to add it that is consistent with the rest of the API.

---

<div class="post-metadata">

### Author: ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)
#### Post date: [September 15, 2021, 3:07pm UTC](https://discourse.julialang.org/t/graph-function-using-algebraofgraphics/67888/3 "2021-09-15T15:07:04Z")

</div>

Thanks Pietro. I am a big fan of the AlgebraOfGraphics (AOG). The refactoring you did to simplify the code base and the user experience is just phenomenal. I am a big user of ggplot2 and increasingly becoming a Julia-user; I find AOG to be a super-clean improvement of ggplot2’s API. Pure gold! Thx again.

PS: All that being said, I would love the ability to graph functions and stay in the AlgebraOfGraphics paradigm - thx for considering it 🙂
