Skip to content

Instantly share code, notes, and snippets.

@bastianolea
Created November 8, 2025 02:08
Show Gist options
  • Select an option

  • Save bastianolea/f51c59c3937544ae5fb235d6eb522fd1 to your computer and use it in GitHub Desktop.

Select an option

Save bastianolea/f51c59c3937544ae5fb235d6eb522fd1 to your computer and use it in GitHub Desktop.
Gráficos interactivos combinados con {ggiraph}
library(ggplot2)
library(ggiraph)
library(dplyr)
library(glue)
library(patchwork)
# gráfico de dispersión
puntos <- iris |>
ggplot() +
aes(Sepal.Length, Sepal.Width, color = Species) +
geom_point_interactive(
size = 3,
aes(data_id = Species,
tooltip = glue("<b>Largo:</b> {Sepal.Length}
<b>Ancho:</b> {Sepal.Width}
<b>Especie:</b> {Species}"))) +
scale_color_brewer(palette = "Dark2") +
guides(color = guide_legend(position = "top"))
# gráfico de barras
barras <- iris |>
group_by(Species) |>
summarize(Sepal.Length = mean(Sepal.Length)) |>
ggplot() +
aes(Species, Sepal.Length, fill = Species) +
geom_col_interactive(
width = 0.4,
aes(data_id = Species,
tooltip = glue("<b>Largo:</b> {Sepal.Length}
<b>Especie:</b> {Species}"))) +
scale_fill_brewer(palette = "Dark2") +
guides(fill = "none")
# combinar ambos gráficos
combinado <- barras + puntos +
plot_layout(widths = c(1, 3))
# interactividad
girafe(ggobj = combinado,
options = list(
opts_tooltip(use_fill = TRUE, css = "background-color: #EAD1FA;
color: black;
font-family: sans-serif;
font-size: 9pt;
padding: 6px; padding-left: 8px; padding-right: 8px;
border-radius: 4px;"),
opts_toolbar(saveaspng = FALSE),
opts_hover(css = "stroke: none;"),
opts_hover_inv(css = "opacity: 0.3;")
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment