
How it works...
Before actually plot in the margins we need a core plot. Step 1 takes care of that. Final result is a rugged scatterplot, rugs are displayed in the top and right sides. Legends are relocated to the bottom. To move the rugs around, tweak sides argument coming from geom_rug().
Argument side has to be inputted with a string. This string may contain the initial letters referencing the sides that shall receive the rug plots. Include 't' for top, 'b' for bottom, 'l' for left and 'r' for right (order does not matter). Most common combinations are 'tr' or 'rt' (top and right); 'bl' or 'lb' (bottom and left); and 'tblr' (top, bottom, left and right)
Legends are moved to the bottom by setting theme( legend.position = 'bottom'). With that we're ensuring that the legends won't be in the way of the marginal histogram to be displayed on the right side.
Afterwards, step 2 draws the entire plot along with the marginals; it uses the ggMarginal() function coming from the ggExtra package. First argument accounts for the central ggplot object, next one holds the data frame. Argument type is picking the type of marginal plots, recipe had histogram chosen. Once we asked for type = histograms; argument bins is also available, it sets the number of bins to be displayed by each marginal histogram.