Code
library(tidyverse)
library(ggiraph) # install.packages('ggiraph')
library(patchwork) # install.packages('patchwork')
library(sf) # install.packages('sf')
ggiraph
Cargar paquetes en R
library(tidyverse)
library(ggiraph) # install.packages('ggiraph')
library(patchwork) # install.packages('patchwork')
library(sf) # install.packages('sf')
# Example data - replace with your data
<- data.frame(
map_data id = 1:3,
lat = c(40, 42, 37),
lon = c(-100, -120, -95),
group = c("A", "B", "C")
)
<- data.frame(
line_data 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
<- ggplot() +
map_plot 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
<- ggplot(line_data, aes(x = time, y = value, group = group, color=group)) +
line_plot geom_line_interactive(aes(data_id = group, tooltip = group))
<- girafe(
combined_plot 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
)
combined_plot
# Read the full world map
<- read_sf("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/world.geojson")
world_sf <- world_sf %>%
world_sf filter(!name %in% c("Antarctica", "Greenland"))
# Create a sample dataset
<- data.frame(
happiness_data 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)
<- ggplot(world_sf, aes(
p1
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)
<- ggplot(world_sf, aes(
p2 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)
<- ggplot() +
p3 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
<- (p1 + p2) / p3 + plot_layout(heights = c(1, 2))
combined_plot
# 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
<- girafe(ggobj = combined_plot)
interactive_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
De igual manera existen otros paquetes interactivos que puedes visitar y emplear: