Skip to content

Instantly share code, notes, and snippets.

@dgkeyes
Created July 20, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgkeyes/4fc6ef8b4b748ae83aa61488c3d9713e to your computer and use it in GitHub Desktop.
Save dgkeyes/4fc6ef8b4b748ae83aa61488c3d9713e to your computer and use it in GitHub Desktop.
# Packages ----------------------------------------------------------------
library(tidyverse)
library(scales)
library(ggiraph)
library(htmlwidgets)
# Themes ------------------------------------------------------------------
source("https://raw.githubusercontent.com/dgkeyes/dk-functions-and-themes/master/themes/tfff_themes.R")
# Data --------------------------------------------------------------------
data <- tibble(
year = seq(1994, 2018, by = 1),
pct = sample(0:100,25,replace=T)/100
)
# Static plot function --------------------------------------------------------------
# Create a function that generates the basic plot. I'll then use it to create the interactive plot as well.
static_plot <- function() {
ggplot(data, aes(x = year,
y = pct,
group = 1)) +
geom_line(color = tfff.dark.green) +
scale_y_continuous(labels = percent,
limits = c(0, 1),
breaks = seq(0, 1, by = .25)) +
scale_x_continuous(breaks = pretty_breaks(n = 10)) +
labs(title = "The retention rate is all over the place",
subtitle = "Because it's randomly generated") +
theme_minimal() +
theme(axis.title = element_blank(),
plot.title = element_text(face = "bold"),
panel.grid.minor.y = element_blank())
}
# Static Plot -------------------------------------------------------------
static_plot() +
geom_point(color = tfff.dark.green,
fill = "white",
shape = 21)
ggsave("plot_static.png",
width = 6,
height = 3)
# Interactive Plot --------------------------------------------------------
p <- static_plot() +
geom_point_interactive(aes(tooltip = paste(year,
" cohort: ",
percent(pct),
sep = "")),
color = tfff.dark.green,
fill = "white",
shape = 21)
p_interactive <- ggiraph(code = print(p),
width = 1,
height = 2)
saveWidget(p_interactive,
file = "plot_interactive.html",
selfcontained = T,
title = "Interactive Plot")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment