# add interactive labels to a ggplot -------
library(ggplot2)
library(ggiraph)
library(gdtools)
register_liberationsans()
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_label_interactive(aes(
tooltip = paste(rownames(mtcars), mpg, sep = "\n")
), family = "Liberation Sans") +
theme_minimal(base_family = "Liberation Sans", base_size = 11)
x <- girafe(
ggobj = p,
dependencies = list(
liberationsansHtmlDependency()
)
)
if (interactive()) {
print(x)
}
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_label_interactive(
aes(fill = factor(cyl), tooltip = paste(rownames(mtcars), mpg, sep = "\n")),
colour = "white",
fontface = "bold",
family = "Liberation Sans"
) +
theme_minimal(base_family = "Liberation Sans", base_size = 11)
x <- girafe(
ggobj = p,
dependencies = list(
liberationsansHtmlDependency()
)
)
if (interactive()) {
print(x)
}
# add interactive texts to a ggplot -------
library(ggplot2)
library(ggiraph)
library(gdtools)
register_liberationsans()
## the data
dataset = mtcars
dataset$label = row.names(mtcars)
dataset$tooltip = paste0(
"cyl: ",
dataset$cyl,
"",
"gear: ",
dataset$gear,
"",
"carb: ",
dataset$carb
)
## the plot
gg_text = ggplot(
dataset,
aes(
x = mpg,
y = wt,
label = label,
color = qsec,
tooltip = tooltip,
data_id = label
)
) +
geom_text_interactive(check_overlap = TRUE, family = "Liberation Sans") +
coord_cartesian(xlim = c(0, 50)) +
theme_minimal(base_family = "Liberation Sans", base_size = 11)
## display the plot
x <- girafe(
ggobj = gg_text,
dependencies = list(
liberationsansHtmlDependency()
)
)
x <- girafe_options(x = x, opts_hover(css = "fill:#FF4C3B;font-style:italic;"))
if (interactive()) {
print(x)
}
Run the code above in your browser using DataLab