6 Examples
6.1 CEO departures
Thank to Martín Pons that kindly shared his code with us.
The following code is an adaptation of Martin’s script (https://github.com/MartinPons/tidytuesday_MartinPons/blob/master/2021-17/ceos.R).
The following packages will be used:
library(ggtext)
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggiraph)
library(glue)
First let’s define some color variables and labels to make the ggplot code lighter and get a clear script.
<- "#894843"
voluntary_col <- "#887d74"
involuntary_col <- "#D7E0DA"
bg_color <- "#1f3225"
font_color <- "#4c6061"
hover_color
<- "CEO DEPARTURES"
title
<- glue("CEO **<span style = 'color:{voluntary_col}'>voluntary</span>** and
subtitle **<span style= 'color:{involuntary_col}'>involuntary</span>** departures
in the 20 *S&P 1500* firms<br>with most CEO rotation between 1993 and 2018")
<- "Data comes from Gentry et al. Facilitated by DatalsPlural. Visualization by Martín Pons | @MartinPonsM" caption
The ggplot theme defined below is used to produce the graphic:
<- theme(
departures_theme text = element_text(color = font_color, family = "Open Sans"),
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_textbox(halign = .5, hjust = .5, family = "Open Sans", size = 8),
plot.caption = element_text(size = 6, hjust = .9),
plot.background = element_rect(fill = bg_color, color = bg_color),
panel.background = element_rect(fill = bg_color, color = bg_color),
axis.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()
)
The data is read and an aggregation is computed.
<- readRDS('data/departures.RDS')
departures departures
coname | main_cause | tooltip | fyear |
---|---|---|---|
character | factor | glue | numeric |
BARRICK GOLD | voluntary | Firm: BARRICK GOLD | 1997 |
BARRICK GOLD | voluntary | Firm: BARRICK GOLD | 2008 |
BARRICK GOLD | voluntary | Firm: BARRICK GOLD | 2007 |
DANA | voluntary | Firm: DANA | 1998 |
n: 148 |
# get number of voluntary and involuntary departures
<- departures %>%
departure_firms_main_cause count(coname, main_cause) %>%
pivot_wider(
names_from = main_cause, values_from = n,
values_fill = 0) %>%
mutate(
firm_label = glue("{coname}:\nVoluntary departures: {voluntary}\nInvoluntary departures: {involuntary}")
) departure_firms_main_cause
coname | voluntary | involuntary | firm_label |
---|---|---|---|
character | integer | integer | glue |
BARNES & NOBLE | 4 | 3 | BARNES & NOBLE: |
BARRICK GOLD | 5 | 3 | BARRICK GOLD : |
BIOLASE | 2 | 5 | BIOLASE: |
CALLAWAY GOLF CO | 4 | 3 | CALLAWAY GOLF CO: |
n: 20 |
It’s now easy to produce the code that will create the interactive (or static) ggplot graphic.
<- ggplot(data = departures, mapping = aes(fyear)) +
gg_departures geom_col_interactive(
mapping = aes(y = 1, fill = main_cause,
tooltip = tooltip, data_id = coname),
color = bg_color, linewidth = 1, show.legend = FALSE) +
geom_text_interactive(
data = departure_firms_main_cause,
aes(x = 1994, y = 9.2, label = firm_label, data_id = coname),
color = hover_color, size = 2.5, hjust = "left", alpha = 0) +
labs(title = title, subtitle = subtitle, caption = caption) +
scale_fill_manual(values = c(voluntary_col, involuntary_col)) +
scale_x_continuous(labels = formatC, breaks = seq(1995, 2015, by=5)) +
+
departures_theme coord_equal()
Now let’s convert the static graphic to a dynamic graphic:
girafe(
ggobj = gg_departures,
bg = bg_color,
options = list(
opts_tooltip(
opacity = 0.8, use_fill = TRUE,
use_stroke = FALSE,
css = "padding:5pt;font-family: Open Sans;font-size:1rem;color:white"),
opts_hover_inv(css = "opacity:0.4"),
opts_toolbar(saveaspng = FALSE),
opts_zoom(max = 1),
opts_hover(
css = girafe_css(
css = glue("fill:{font_color};"),
text = glue("stroke:none;fill:{font_color};fill-opacity:1;")
))
) )