# Plotting histogram overlayed with kernel estimator

I would like to plot a KDE from KernelDensity onto the frequency histogram of the corresponding raw data. Is this possible? Can you provide example code, if so?

Thanks!

Joffre

0

Hi Joffre,

It is possible to overlay a histogram onto a KDE of the corresponding raw data. The easiest way to do this is to:

1. Plot the KDE using the kernelDensity function.
``````/*
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");

// Example One:
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);

// Add histogram of raw data
// using frequencies with 50 bins

Note that this will plot the histogram on the same y-axis as the KDE. For better appearance, you can use a `plotControl` structure to plot the histogram on the right y-axis:

``````/*
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");

// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);

// Use plot control structure to plot
// new graph on right axis
struct plotControl ctl;
ctl = plotGetDefaults("bar");

// Specify to plot histogram
// on right y-axis
plotSetWhichYAxis(&ctl, "right");

// Add histogram of raw data
// using frequency with 100 bins

Eric

105

0

Eric, thank you! This is very helpful, particularly the second example. That solves the messy scaling issue across the empirical distribution and the kernel function.

Joffre

0

Hi Joffre,

It is possible to overlay a histogram onto a KDE of the corresponding raw data. The easiest way to do this is to:

1. Plot the KDE using the kernelDensity function.
``````/*
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");

// Example One:
// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);

// Add histogram of raw data
// using frequencies with 50 bins

Note that this will plot the histogram on the same y-axis as the KDE. For better appearance, you can use a `plotControl` structure to plot the histogram on the right y-axis:

``````/*
*/
fname = getGAUSSHome("examples/winevolatileacidity.csv");

// Full sample normal density
struct kernelDensityResults krslt1;
krslt1 = kernelDensity(dataset[., "volatile acidity"]);

// Use plot control structure to plot
// new graph on right axis
struct plotControl ctl;
ctl = plotGetDefaults("bar");

// Specify to plot histogram
// on right y-axis
plotSetWhichYAxis(&ctl, "right");

// Add histogram of raw data
// using frequency with 100 bins

0

Eric, thank you! This is very helpful, particularly the second example. That solves the messy scaling issue across the empirical distribution and the kernel function.

Joffre