# ggdensity: A new R package for plotting high-density regions

Written by Matt Dancho

As data scientists, it can be downright impossible to drill into messy data. Fortunately, thereâ€™s a new R package that helps us focus on a â€śhigh-density regionâ€ť, which is simply an area in a scatter plot defined by a high percentage of the data points. Itâ€™s called ggdensity.

High Density Regions on a Scatter Plot

In this R-tip, Iâ€™m going to show you how to hone in on high-density regions under 5-minutes:

1. Learn how to make high-density scatter plots with ggdensity
2. BONUS: Make faceted density plots to drill into over-plotted high-density region data

# R-Tips Weekly

Here are the links to get set up. đź‘‡

# Video Tutorial

I have a companion video tutorial that shows even more secrets (plus mistakes to avoid).

# What you make in this R-Tip

By the end of this tutorial, youâ€™ll use of high density regions to make insights from groups within your data. For example, here we can see where each Class of Vehicle compares in terms of engine displacement (displ) and highway fuel economy (hwy), answering questions like:

• Is vehicle class a good way to describe vehicle clusters?
• Which vehicle classes have the greatest variation in highway fuel economy versus displacement?
• Which vehicle classes have the highest / lowest highway fuel economy?

Do you see how powerful ggdensity is?

Uncover insights with ggdensity

# Thank You Developers.

Before we move on, please recognize that ggdensity was developed by James Otto, Doctoral Candidate at the Department of Statistical Science, Baylor University. Thank you for everything you do! Also, the full documentation for ggdensity can be accessed here.

## Before we get started, get the R Cheat Sheet

ggdensity is great for extending ggplot2 with advanced features. But, youâ€™ll need to learn ggplot2 to take full advantage. For these topics, Iâ€™ll use the Ultimate R Cheat Sheet to refer to ggplot2 code in my workflow.

### Quick Example:

Now youâ€™re ready to quickly reference the ggplot2 cheat sheet. This shows you the core plotting functions available in the ggplot library.

Onto the tutorial.

# ggdensity Tutorial

Letâ€™s dive into using ggdensity so we can show you how to make high-density regions on your scatter plots.

Important: All of the data and code shown can be accessed through our Business Science R-Tips Project.

Plus I have a surprise at the end (for everyone)!

## đź’ˇ Step 1: Load the Libraries and Data

### First, run this code to load the R libraries:

Load tidyverse , tidyquant, and ggdensity.

### Next, run this code to pull in the data.

Weâ€™ll read in the mpg data set that was comes with ggplot2.

We want to understand how highway fuel economy relates to engine size (displacement) and to see if there are clusters by vehicle class.

## đź’ˇ Step 2: Make a basic ggplot

Next, make a basic ggplot using the following code. This creates a scatter plot with the colors that change by vehicle class. I wonâ€™t go into all of the mechanics, but you can download my R cheat sheet to learn more about ggplot and the grammar of graphics.

Hereâ€™s what the plot looks like. Do you see how itâ€™s really tough to pull out the clusters in there? Each of the points overlap which makes understanding the group structure in the data very tough.

## Step 3: Add High Density Regions

Ok, now that we have a basic scatter plot, we can make a quick alteration by adding high density regions that capture 90% and 50% of the data. We use geom_hdr(probs = c(0.9, 0.5, alpha = 0.35) to accomplish the next plot.

Letâ€™s see what we have here.

We can now see where the clusters have the highest density. But thereâ€™s still a problem called â€śoverplottingâ€ť, which is when too many graphics get plot on top of each other.

# đź’ˇ BONUS: Overplotting solved!

Hereâ€™s the problem weâ€™re facing: overplotting. We simply have too many groups that are too close together. Letâ€™s see how to fix this.

The fix is pretty simple. Just use facetting from ggplot2.

And, voila! We can easily inspect the clusters by vehicle class.

# đź’ˇ Conclusions

You learned how to use the ggdensity library to create high-density regions that help us understand the clusters within our data. Great work! But, thereâ€™s a lot more to becoming a Business Scientist.

If youâ€™d like to become a Business Scientist (and have an awesome career, improve your quality of life, enjoy your job, and all the fun that comes along), then I can help with that.

Iâ€™ve helped 6,107+ students learn data science for business from an elite business consultantâ€™s perspective.

Iâ€™ve worked with Fortune 500 companies like S&P Global, Apple, MRM McCann, and more.

And I built a training program that gets my students life-changing data science careers (donâ€™t believe me? see my testimonials here):

# Whenever you are ready, hereâ€™s the system they are taking:

Hereâ€™s the system that has gotten aspiring data scientists, career transitioners, and life long learners data science jobs and promotionsâ€¦

P.S. - Samantha landed her NEW Data Science R Developer job at CVS Health (Fortune 500). This could be you.