Graphe Interactif

Code

When you click the Render button a presentation will be generated that includes both content and the output of embedded code. You can embed code like this:

library(ggiraph)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mtcars_db <- rownames_to_column(mtcars, var = "carname")

myplot <- ggplot(
  data = mtcars_db,
  mapping = aes(
    x = disp, y = qsec,
    # here we add interactive aesthetics
    tooltip = carname, data_id = carname
  )
) +
  geom_point_interactive(
    size = 3, hover_nearest = TRUE
  )

interactive_plot <- girafe(ggobj = myplot)
interactive_plot
library(patchwork)

mtcars_db <- rownames_to_column(mtcars, var = "carname")

# First plot: Scatter plot
scatter <- ggplot(
  data = mtcars_db,
  mapping = aes(
    x = disp, y = qsec,
    tooltip = carname, data_id = carname
  )
) +
  geom_point_interactive(
    size = 3, hover_nearest = TRUE
  ) +
  labs(
    title = "Displacement vs Quarter Mile",
    x = "Displacement", y = "Quarter Mile"
  ) +
  theme_bw()

# Second plot: Bar plot
bar <- ggplot(
  data = mtcars_db,
  mapping = aes(
    x = reorder(carname, mpg), y = mpg,
    tooltip = paste("Car:", carname, "<br>MPG:", mpg),
    data_id = carname
  )
) +
  geom_col_interactive(fill = "skyblue") +
  coord_flip() +
  labs(
    title = "Miles per Gallon by Car",
    x = "Car", y = "Miles per Gallon"
  ) +
  theme_bw()

# Combine the plots using patchwork
combined_plot <- scatter + bar +
  plot_layout(ncol = 2)

# Create a single interactive plot with both subplots
interactive_plot <- girafe(ggobj = combined_plot)

# Set options for the interactive plot
interactive_plot <- girafe_options(
  interactive_plot,
  opts_hover(css = "fill:cyan;stroke:black;cursor:pointer;"),
  opts_selection(type = "single", css = "fill:red;stroke:black;")
)
interactive_plot
# library
library(ggplot2)
library(ggiraph)
library(tidyverse)

# Create data
data <- read.csv("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/dataConsumerConfidence.csv") %>%
  mutate(date = lubridate::my(Time)) %>%
  select(-Time) %>%
  pivot_longer(!date, names_to = "country", values_to = "value") %>%
  na.omit() %>%
  mutate(country = as.factor(country))

#-------------------------

plot <- data %>%
  ggplot(mapping = aes(
    x = date,
    y = value,
    color = country,
    tooltip = country,
    data_id = country
  )) +
  geom_line_interactive(hover_nearest = TRUE) +
  theme_classic()

interactive_plot <- girafe(ggobj = plot)
interactive_plot
plot <- data %>%
  ggplot(mapping = aes(
    x = date,
    y = value,
    color = country,
    group = country,
    tooltip = paste("Country:", country, "<br>Date:", date, "<br>Value:", round(value, 2)),
    data_id = country
  )) +
  geom_line_interactive(size = 1.2, hover_nearest = TRUE) +
  geom_point_interactive(aes(size = value), alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size_continuous(range = c(1, 2)) +
  theme_minimal(base_size = 14) +
  labs(
    title = "Interactive Country Data Visualization",
    subtitle = "Try to hover and click on the lines!",
    caption = "R-Graph-Gallery.com",
    x = "Date",
    y = "Consumer Confidence"
  ) +
  theme(
    plot.title = element_text(hjust = 0.5, size = 20, face = "bold"),
    plot.subtitle = element_text(hjust = 0.5, size = 16, face = "italic"),
    legend.position = "none",
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "ivory", color = NA),
    plot.background = element_rect(fill = "ivory", color = NA)
  )

interactive_plot <- girafe(ggobj = plot)

hover_css <- "
  stroke: black;
  stroke-width: 1px;
  r: 8px;
  transition: all 0.3s ease;
"

tooltip_css <- "
  background-color: #2C3E50;
  color: #ECF0F1;
  padding: 10px;
  border-radius: 5px;
  font-family: 'Arial', sans-serif;
  font-size: 14px;
  box-shadow: 0px 0px 10px rgba(0,0,0,0.5);
"

interactive_plot <- girafe_options(
  interactive_plot,
  opts_hover(css = hover_css),
  opts_tooltip(css = tooltip_css, use_fill = TRUE),
  opts_selection(type = "multiple", only_shiny = FALSE),
  opts_zoom(min = 0.5, max = 2),
  opts_toolbar(saveaspng = TRUE, position = "topright", pngname = "country_data_plot"),
  opts_sizing(rescale = TRUE)
)
interactive_plot