MyNixOS website logo
Description

Scales that Focus Specific Levels in your ggplot().

A 'ggplot2' extension that provides tools for automatically creating scales to focus on subgroups of the data plotted without losing other information.

ggfocus

BuildStatus CRANstatus

Introduction

In data analysis, one may want to visualize data for a specific subgroup of observations. Simply filtering out observations that do not belong to the subgroup may take the information out of context. Therefore, it is necessary to have tools that allow the analyst to draw attention (focus) on that subgroup within the complete relevant information available.

While ggplot2 is a great tool for data visualization in general, constructing graphics that focus on those subgroups may need very troublesome manipulation of data and graphical scales (for example colors) together, i.e. setting low alpha for unimportant observations, coloring things in a way that highlights the focused subgroup, etc.

ggfocus allows you to build graphics that focus on those specific subgroups by doing the scale manipulation automatically while keeping all the flexibility from ggplot. The idea behind this package is from this issue from tidyverse/ggplot2.

Installing ggfocus

The package is available on CRAN, but you can also install the latest version from Github with devtools.

devtools::install_github("Freguglia/ggfocus") # Latest version
install.packages("ggfocus") # CRAN version

Usage

The workflow of ggfocus is the same as any ggplot graphic with the addition of the focus scales family of functions:

  • scale_color_focus(focus_levels = character(0), color_focus = NULL, color_other = "black", palette_focus = "Set1")
  • scale_fill_focus(focus_levels = character(0), color_focus = NULL, color_other = "gray", palette_focus = "Set1")
  • scale_alpha_focus(focus_levels = character(0), alpha_focus = 1, alpha_other = .05)
  • scale_linetype_focus(focus_levels = character(0), linetype_focus = 1, linetype_other = 3)
  • scale_shape_focus(focus_levels = character(0), shape_focus = 8, shape_other = 1)
  • scale_size_focus(focus_levels = character(0), size_focus = 3, size_other = 1)

The user should map the variable with the grouping variable to all the aes used to highlight observations and then use these functions to automatically create scales that highlight a specified group of observations.

Both the selected and unselected groups characteristics are customizable with the parameters of focus scales. See the examples below.

Examples

Creating an example dataset.

library(ggfocus)
set.seed(1)
# Create an example dataset
df <- data.frame(u1 = runif(300), 
                 u2 = runif(300),
                 grp = sample(LETTERS[1:10], 300, replace = TRUE))
dplyr::glimpse(df)
#> Observations: 300
#> Variables: 3
#> $ u1  <dbl> 0.26550866, 0.37212390, 0.57285336, 0.90820779, 0.20168193, 0.898…
#> $ u2  <dbl> 0.67371223, 0.09485786, 0.49259612, 0.46155184, 0.37521653, 0.991…
#> $ grp <fct> C, E, B, E, E, C, J, B, G, H, B, J, G, A, I, H, F, F, C, J, A, F,…

Visualizing with focus on observations such that grp is A or B.

ggplot(df, aes(x = u1, y = u2, color = grp, alpha = grp)) +
  geom_point() +
  scale_color_focus(c("A", "B"), color_focus = c("blue", "red")) +
  scale_alpha_focus(c("A", "B"))

Interaction with other extensions

Since ggfocus creates the focused visualization solely by controlling scales, other ggplot extensions and types of graphics can interact with it the same way, an example with the maps package is shown below.

library(maps)
wm <- map_data("world")
ggplot(wm, aes(x=long, y = lat, group = group, fill = region)) + 
  geom_polygon(color="black") +
  theme_void() +
  scale_fill_focus(c("Brazil", "Canada", "Australia", "India"), color_other = "gray")

Metadata

Version

1.0.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows