ggplot2: Radar Love

This is part eight of a series on ggplot2.

As I bring this series to a close, I want to show off one last geom:  the radar chart.  I’m a fan of radar charts and you can build them natively with ggplot, but there is also an extension called ggradar.

This brings me to a bit of a sidebar.  During the course of this series, I’ve looked at several ggplot2 extensions:  ggthemes, ggrepel, and cowplot.  These extensions and more are available at the ggplot2 extensions gallery.  There are some quite good extensions here and if you’re struggling to conceptualize a graph in ggplot2, this might give you other alternatives.

Anyhow, let’s get our radar chart on.

Building A Radar Chart

The first trick to a good radar chart is having normalized data, where everything is scaled on a common range.  Typically, we standardize data on a range from 0 to 1, where 1 is the largest value in the set and the point value for each entity is its value divided by the largest value in the set.  So for example, in the gapminder data set, Norway’s per-capita GDP in 2007 is $49,357.19.  The United States is 4th at $42,951.65.  Standardized values would set Norway = 1.0 and the US at 0.87.

The second trick to a good radar chart is to have several variables of interest and a relatively small number of observations to track.  With gapminder, we can group by continent so we only have five observations, and can track a few variables for the year 2007:  number of countries, min and max life expectancy, average population, and min and max GDP per capita.

Here’s the code:

devtools::install_github("ricardo-bion/ggradar", dependencies=TRUE)
library(tidyverse)
library(gapminder)

standardize <- function(x){
    x/max(x)
}

radar_data <- gapminder %>%
    filter(year == 2007) %>%
    group_by(continent) %>%
    summarize(
        n = n(),
        minLife = min(lifeExp),
        maxLife = max(lifeExp),
        meanPop = mean(pop),
        minGdpPercap = min(gdpPercap),
        maxGdpPercap = max(gdpPercap)
    ) %>%
    mutate_each_(
        funs(standardize(.) %>% as.vector),
        vars=c("n", "minLife", "maxLife", "meanPop", "minGdpPercap", "maxGdpPercap")
    )

ggradar::ggradar(
    plot.data = radar_data,
    font.radar = "Gill Sans MT",
    grid.label.size = 6,
    axis.label.size = 5,
    group.point.size = 3,
    group.line.width = 1,
    legend.text.size = 12
)
50_radar

Things I can’t quit:  radar charts.

First, we need to install ggradar and load our relevant libraries. Then, I create a quick standardization function which divides our variable by the max value of that variable in the vector. It doesn’t handle niceties like divide by 0, but we won’t have any zero values in our data frames.

The radar_data data frame starts out simple: build up some stats by continent. Then I call the mutate_each_ function to call standardize for each variable in the vars set. mutate_each_ is deprecated and I should use something different like mutate_at, but this does work in the current version of ggplot2 at least.

Finally, I call the ggradar() function. This function has a large number of parameters, but the only one you absolutely need is plot.data. I decided to change the sizes because by default, it doesn’t display well at all on Windows.

What we end up with is a fun radar chart, letting us see how each continent stacks up.  We can see that Africa has the most countries and Oceania the least—Oceania only has two countries in the data set, Australia and New Zealand.

I should note that the “minimum” variables are for the lowest value within that continent, so the lowest GDP per capita in Oceania is significantly higher than the lowest GDP per capita in any other continent.  The easiest way to think about it is to consider these the best of the worst per continent.  The other variables work as you’d expect.

Conclusion

In today’s post, we took a look at a ggplot2 extension, ggradar.  These extensions provide us an easy way of getting functionality which we might possibly only get with great difficulty, or maybe not at all.  You don’t need to use extensions to create good visuals, but knowing where they are and when to use them can make a big difference.

Advertisements

3 thoughts on “ggplot2: Radar Love

  1. This radar chart gave me an idea for work. I like the Gill Sans MT font you used. Thank you this cool ggplot series. You might submit your blog to r-bloggers.com and/or rweekly.org if you have not already. You may have to submit individual posts to rweekly.org if you are not tagging your R-related posts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s