6  Otros ejemplos con ggiraph

Cargar paquetes en R

Code
library(tidyverse)
library(ggiraph) # install.packages('ggiraph')
library(patchwork) # install.packages('patchwork')
library(sf) # install.packages('sf')

6.1 Ejemplo 1

Code
# Example data - replace with your data
map_data <- data.frame(
  id = 1:3,
  lat = c(40, 42, 37),
  lon = c(-100, -120, -95),
  group = c("A", "B", "C")
)

line_data <- data.frame(
  id = rep(1:3, each = 10),
  time = rep(seq(as.Date("2021-01-01"), by = "1 month", length.out = 10), 3),
  value = rnorm(30),
  group = rep(c("A", "B", "C"), each = 10)
)

# Map with interactive points
map_plot <- ggplot() +
  borders("world", colour = "gray80", fill = "gray90") +  # Add a world map background
  geom_point_interactive(data = map_data, aes(x = lon, y = lat, size = 5, color=group, tooltip = group, data_id = group)) +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_sf(xlim = c(-130, -65), ylim = c(10, 75)) 


# Line chart with interactive lines
line_plot <- ggplot(line_data, aes(x = time, y = value, group = group, color=group)) +
  geom_line_interactive(aes(data_id = group, tooltip = group))

combined_plot <- girafe(
  ggobj = map_plot + plot_spacer() + line_plot + plot_layout(widths = c(0.35, 0, 0.65)),
  options = list(
    opts_hover(css = ''),
    opts_hover_inv(css = "opacity:0.1;"), 
    opts_sizing(rescale = FALSE)
  ),
  height_svg = 4,
  width_svg = 12
)
Code
combined_plot

6.2 Ejemplo 2

Code
# Read the full world map
world_sf <- read_sf("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/world.geojson")
world_sf <- world_sf %>%
  filter(!name %in% c("Antarctica", "Greenland"))

# Create a sample dataset
happiness_data <- data.frame(
  Country = c(
    "France", "Germany", "United Kingdom",
    "Japan", "China", "Vietnam",
    "United States of America", "Canada", "Mexico"
  ),
  Continent = c(
    "Europe", "Europe", "Europe",
    "Asia", "Asia", "Asia",
    "North America", "North America", "North America"
  ),
  Happiness_Score = rnorm(mean = 30, sd = 20, n = 9),
  GDP_per_capita = rnorm(mean = 30, sd = 20, n = 9),
  Social_support = rnorm(mean = 30, sd = 20, n = 9),
  Healthy_life_expectancy = rnorm(mean = 30, sd = 20, n = 9)
)

# Join the happiness data with the full world map
world_sf <- world_sf %>%
  left_join(happiness_data, by = c("name" = "Country"))

# Create the first chart (Scatter plot)
p1 <- ggplot(world_sf, aes(
  GDP_per_capita,
  Happiness_Score,
  tooltip = name,
  data_id = Continent,
  color = Continent
)) +
  geom_point_interactive(data = filter(world_sf, !is.na(Happiness_Score)), size = 4) +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
  )

# Create the second chart (Bar plot)
p2 <- ggplot(world_sf, aes(
  x = reorder(name, Happiness_Score),
  y = Happiness_Score,
  tooltip = name,
  data_id = Continent,
  fill = Continent
)) +
  geom_col_interactive(data = filter(world_sf, !is.na(Happiness_Score))) +
  coord_flip() +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
  )

# Create the third chart (choropleth)
# Create the third chart (choropleth)
p3 <- ggplot() +
  geom_sf(data = world_sf, fill = "lightgrey", color = "lightgrey") +
  geom_sf_interactive(
    data = filter(world_sf, !is.na(Happiness_Score)),
    aes(fill = Continent, tooltip = name, data_id = Continent)
  ) +
  coord_sf(crs = st_crs(3857)) +
  theme_void() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
  )

# Combine the plots
combined_plot <- (p1 + p2) / p3 + plot_layout(heights = c(1, 2))

# Create CSS
tooltip_css <- "
  border-radius: 12px;
  color: #333;
  background-color: white;
  padding: 10px;
  font-size: 14px;
  transition: all 0.5s ease-out;
"

hover_css <- "
  filter: brightness(75%);
  cursor: pointer;
  transition: all 0.5s ease-out;
  filter: brightness(1.15);
"

# Create the interactive plot
interactive_plot <- girafe(ggobj = combined_plot)
# Add interactivity
interactive_plot <- interactive_plot %>%
  girafe_options(
    opts_hover(css = hover_css),
    opts_tooltip(css = tooltip_css),
    opts_hover_inv(css = "opacity:0.3; transition: all 0.2s ease-out;")
  )
interactive_plot
Otras graficas

De igual manera existen otros paquetes interactivos que puedes visitar y emplear:

Chapter 13 Interactive Graphs

6.3 Referencias