yutannihilation / gghighlight

Highlight points and lines in ggplot2

Home Page:https://yutannihilation.github.io/gghighlight/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use facet variables for grouping

petermacp opened this issue · comments

Is it possible to specify a single highlight colour across facets, whilst retaining the unhighlighted geoms?

Thanks!

library(tidyverse)
library(gapminder)
library(gghighlight)

df <- gapminder

df2 <- df %>%
  filter(str_detect(country, "^B") |
           str_detect(country, "^C") |
           str_detect(country, "^D"))

#This shows the unhighlighted lines, but has a different colour per highlighted line
df2 %>%
  ggplot() +
  geom_line(aes(y=lifeExp, x=year, colour=country), size=0.5) +
  geom_point(aes(y=lifeExp, x=year, colour=country), size=0.5) +
  facet_wrap(~country, ncol = 5) +
  gghighlight() +
  theme_minimal()
#> label_key: country
#> Too many data series, skip labeling

#This shows only the highlighted line with the correct colour
df2 %>%
  ggplot() +
  geom_line(aes(y=lifeExp, x=year), colour="#1D4C8C", size=0.5) +
  geom_point(aes(y=lifeExp, x=year), colour="#1D4C8C", size=0.5) +
  facet_wrap(~country, ncol = 5) +
  gghighlight() +
  theme_minimal()

Created on 2020-04-09 by the reprex package (v0.3.0)

In this case, you need to specify group. You might notice or not, in your second example, gghighlight() does nothing; we only see the lines on the faceted data.

library(tidyverse)
library(gapminder)
library(gghighlight)

df <- gapminder

df2 <- df %>%
  filter(str_detect(country, "^B") |
    str_detect(country, "^C") |
    str_detect(country, "^D"))

df2 %>%
  ggplot() +
  geom_line(aes(y = lifeExp, x = year, group = country), colour = "#1D4C8C", size = 0.5) +
  geom_point(aes(y = lifeExp, x = year, group = country), colour = "#1D4C8C", size = 0.5) +
  facet_wrap(~country, ncol = 5) +
  gghighlight() +
  theme_minimal()
#> label_key: country
#> Too many data series, skip labeling

Created on 2020-04-12 by the reprex package (v0.3.0)

Thank you - easy when you know how! Much appreciated!