Created
October 6, 2023 10:32
-
-
Save teunbrand/d0c7a0a2ee97807a10f1546deae80195 to your computer and use it in GitHub Desktop.
Playing with {ggtext} and {epoxy}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(epoxy) | |
library(ggplot2) | |
library(ggtext) | |
# Creating an epoxy theme element | |
element_epoxy <- function(...) { | |
el <- ggtext::element_markdown(...) | |
class(el) <- union("element_epoxy", class(el)) | |
el | |
} | |
# Rendering the epoxy element is simply pulling the label through the epoxy | |
# function and forwarding it to element_markdown's element_grob method. | |
element_grob.element_epoxy <- function(element, label = "", ...) { | |
label <- vapply(label, epoxy, character(1)) | |
NextMethod() | |
} | |
# Basic plot with epoxied x-axis | |
p <- ggplot(mpg, aes(displ, hwy)) + | |
geom_point() + | |
theme(axis.title.x.bottom = element_epoxy()) | |
# Test plot | |
p + labs(x = "{.italic 'Engine Displacement'}") | |
# Helper function for setting span styles | |
span_style <- function(style) { | |
force(style) | |
function(x) paste0("<span style = '", style, "'>", x, "</span>") | |
} | |
# Set new transformers | |
epoxy_transform_set( | |
.red = span_style("color:red"), | |
.blue = span_style("color:blue"), | |
.serif = span_style("font-family:serif"), | |
.papyrus = span_style("font-family:Papyrus") | |
) | |
# Using new transformers | |
p + labs(x = "{.red .serif 'Engine Displacement'}") | |
p + labs(x = "{.papyrus .blue 'Engine Displacement'}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rendered as a reprex with ragg device:
Created on 2023-10-06 with reprex v2.0.2