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.
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:
ggfocus() uses pipes (%>%) and returns a modified version of the ggplot object.scale_*_focus() uses the usual grammar from ggplot2 (+) and behaves exactly like most other scale creation functions.(Please note that ggfocus refers to the package and ggfocus() refers to the function in the package)
Let’s use the gapminder dataset to create examples of how each approach can be used:
ggplot that shows the lifeExp curve over time for each country and color those with the continent factor variable.library(ggplot2)
library(ggfocus)
library(gapminder)
p1 <- ggplot(gapminder, aes(x = year, y = lifeExp, color = continent, group = country)) + geom_line()
p1Europe continent with ggfocus(). This allows us to focus our visualization in European contries without losing the information of what happens in the other countries. I recommend reading the function documentation for information about the parameters.p1 %>% ggfocus(var = continent, focus_levels = c("Europe"), focus_aes = c("color","alpha"),
color_focus = "red", alpha_focus = 1, alpha_other = 0.15)scale functions#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"))fill aesthetics is also availablep3 <- 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")gghighlight might be more suitable for some problems.