These sketch geoms have interactive versions for the ggiraph package:
- points:
geom_sketch_point_interactive() - bars:
geom_sketch_col_interactive(),geom_sketch_bar_interactive(),geom_sketch_histogram_interactive() - rectangles and tiles:
geom_sketch_rect_interactive(),geom_sketch_tile_interactive() - shapes and areas:
geom_sketch_polygon_interactive(),geom_sketch_area_interactive(),geom_sketch_ribbon_interactive() - lines:
geom_sketch_line_interactive(),geom_sketch_path_interactive(),geom_sketch_step_interactive(),geom_sketch_segment_interactive() - boxes and labels:
geom_sketch_boxplot_interactive(),geom_sketch_text_interactive() - maps:
geom_sketch_sf_interactive()
They take every argument of their sketch geom, plus ggiraph’s
interactive aesthetics: tooltip, data_id,
onclick, hover_css, selected_css,
tooltip_fill and hover_nearest. Build the plot
as usual, then hand it to girafe().
Tooltips
Hover over a bar to see its tooltip. Every stroke of the bar carries
it, and an invisible area sits under the hatching, so the tooltip shows
anywhere inside the bar, not only on its lines. A shared
data_id highlights the whole bar together.
sales <- data.frame(
item = c("pens", "paper", "ink", "nibs"),
sold = c(12, 30, 7, 18)
)
p <- ggplot(sales, aes(item, sold)) +
geom_sketch_col_interactive(
aes(tooltip = paste0(item, ": ", sold, " sold"), data_id = item),
fill = "#7BAFD4"
) +
labs(x = NULL, y = "Units sold") +
theme_sketch()
girafe(ggobj = p, width_svg = 6, height_svg = 3.5)geom_sketch_bar_interactive() counts like
geom_sketch_bar(), so a tooltip can use the computed
count:
p <- ggplot(mpg, aes(class)) +
geom_sketch_bar_interactive(
aes(tooltip = after_stat(paste(count, "cars")), data_id = class),
fill_style = "cross_hatch"
) +
labs(x = NULL, y = NULL) +
theme_sketch()
girafe(ggobj = p, width_svg = 7, height_svg = 3.5)Highlighting a group
Points that share a data_id light up together. Here each
species is one id, and opts_hover_inv() fades everything
else while one is hovered.
p <- ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) +
geom_sketch_point_interactive(
aes(tooltip = Species, data_id = Species, shape = Species),
size = 2.5
) +
scale_colour_sketch() +
theme_sketch()
girafe(
ggobj = p, width_svg = 6, height_svg = 4,
options = list(
opts_hover(css = "stroke:#c0392b;"),
opts_hover_inv(css = "opacity:0.2;")
)
)Lines
A line takes the tooltip of its group, and every stroke along it carries it. A sketched line is thin and wobbly, so each one also gets an invisible band, about an eighth of an inch wide, that reaches its tooltip. That holds for every medium, chalk and spray included.
rates <- subset(economics_long, variable %in% c("psavert", "uempmed"))
rates$series <- ifelse(rates$variable == "psavert",
"Personal savings rate (%)", "Median weeks unemployed")
p <- ggplot(rates, aes(date, value, colour = series)) +
geom_sketch_line_interactive(aes(tooltip = series, data_id = series)) +
scale_colour_sketch(name = NULL) +
labs(x = NULL, y = NULL) +
theme_sketch() +
theme(legend.position = "bottom")
girafe(
ggobj = p, width_svg = 7, height_svg = 3.8,
options = list(
opts_hover(css = "stroke:#222222;"),
opts_hover_inv(css = "opacity:0.25;")
)
)Boxes and labels
A box takes its group’s tooltip on its outline, whiskers, median and
outliers, and an unfilled box can be hovered anywhere inside. Each label
of geom_sketch_text_interactive() takes its own.
p <- ggplot(mpg, aes(class, hwy)) +
geom_sketch_boxplot_interactive(
aes(tooltip = after_stat(paste0("median ", middle, " mpg")),
data_id = class)
) +
labs(x = NULL, y = "Highway mpg") +
theme_sketch()
girafe(ggobj = p, width_svg = 7, height_svg = 3.5)
cars <- mtcars[c("Mazda RX4", "Honda Civic", "Fiat 128", "Merc 240D",
"Duster 360", "Cadillac Fleetwood"), ]
cars$model <- rownames(cars)
p <- ggplot(cars, aes(wt, mpg)) +
geom_sketch_point(size = 2) +
geom_sketch_text_interactive(
aes(label = model, tooltip = paste0(model, ": ", hp, " hp"),
data_id = model),
hjust = "inward", vjust = -0.9, size = 4.5
) +
scale_x_continuous(expand = expansion(mult = 0.1)) +
scale_y_continuous(expand = expansion(mult = 0.15)) +
theme_sketch()
girafe(ggobj = p, width_svg = 6, height_svg = 3.8)girafe() keeps text as SVG text, which the browser draws
in its own fonts, so a handwriting face the browser does not have falls
back to a plain one, as it may here. To keep the hand in the browser,
the font has to reach it too: for example, a web font under the family
name the layer draws with, added through girafe()’s
dependencies argument.
Maps
geom_sketch_sf_interactive() gives each feature of an sf
map its own tooltip, whether it is a polygon, a line or a point.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) +
geom_sketch_sf_interactive(
aes(fill = BIR74 / 1000, tooltip = paste0(NAME, ": ", BIR74, " births"),
data_id = NAME),
linewidth = 0.25
) +
scale_fill_viridis_c("Births\n(000s)", option = "magma", direction = -1) +
theme_void()
girafe(ggobj = p, width_svg = 8, height_svg = 3.2)Styling the hover
ggiraph’s default hover turns lines orange and fills shapes orange. A
sketch is drawn with lines (outlines, hatching, glyph strokes), so a
hover style that sets stroke recolours the whole drawing,
as in the species plot above. The invisible areas under shapes and the
bands along lines keep their fill hidden whatever colour the style gives
them. To style the kinds of element apart, build the CSS with
girafe_css(): sketch strokes are its line
elements, and solid and watercolour fills are its area
elements.
girafe(
ggobj = p,
options = list(opts_hover(css = girafe_css(
css = "cursor:pointer;",
line = "stroke:#c0392b;stroke-width:1.5pt;",
area = "fill:#f6d5cf;"
)))
)Outside girafe(), on the screen or in a PNG, PDF or SVG
file, the interactive geoms draw exactly as their sketch geoms do, so
one plot serves both.
