A quick guide to ggfocus

Victor Freguglia

09/08/2018

Introduction

ggfocus is a ggplot2 extension that allows you to eaily create scales that focus on specific factor levels. This can be achieved in two ways: - Using the ggfocus() function with an existing ggplot object; or - Adding the scales for the aes (color, fill, alpha) you want to use in order to highlight those levels.

When doing data visualization, we often use colors as a third dimension for categorical variables. This package is specially useful for data with too many levels of a categorical variable where only a few of those levels is of interest, mainly because we cannot fully process the information from plots with more than maybe 6 or 8 colors.

In this type of situation, one needs to create new column(s) to select which categories to focus on. The goal of ggfocus is to make this step automatical, saving the user’s time.

Usage

As mentioned in the previous section, you can either use ggfocus() to automatically highlight categories in an existing ggplot object, or adding the focusing scales with scale_*_focus() functions. The important thing to understand is:

(Please note that ggfocus refers to the package and ggfocus() refers to the function in the package)

Examples

Let’s use the gapminder dataset to create examples of how each approach can be used:

library(ggplot2)
library(ggfocus)
library(gapminder)

p1 <- ggplot(gapminder, aes(x = year, y = lifeExp, color = continent, group = country)) + geom_line()
p1

p1 %>% ggfocus(var = continent, focus_levels = c("Europe"), focus_aes = c("color","alpha"),
          color_focus = "red", alpha_focus = 1, alpha_other = 0.15)

#p1 + scale_alpha_focus(focus_levels = "Europe") # This throws an error because alpha isn't mapped by any variable.
p1 + aes(alpha = continent) + scale_alpha_focus("Europe", 1, 0.15) + scale_color_focus("Europe")

p2 <- ggplot(gapminder, aes(x = year, y = lifeExp, color = country, group = country)) + geom_line()
p2 + scale_color_focus(focus_levels = c("Brazil","Argentina")) + aes(alpha = country) +
  scale_alpha_focus(c("Brazil","Argentina"))

# Same as 
# p2 %>% ggfocus(var = country, focus_levels = c("Brazil","Argentina"))
p3 <- ggplot(gapminder, aes(x = log(pop), fill = continent)) + geom_histogram()
p3
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

p3 + scale_fill_focus(c("Asia"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Same as
# p3 %>% ggfocus(var = continent, focus_levels = "Asia", focus_aes = "fill", color_other = "gray")

Additional Comments