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 aestheticstooltip = 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 plotscatter <-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 plotbar <-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 patchworkcombined_plot <- scatter + bar +plot_layout(ncol =2)# Create a single interactive plot with both subplotsinteractive_plot <-girafe(ggobj = combined_plot)# Set options for the interactive plotinteractive_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