Skip to content

Instantly share code, notes, and snippets.

@zachcp
Created October 5, 2022 18:44
Show Gist options
  • Save zachcp/f4cca1f8d4878df2c54eb43659bff472 to your computer and use it in GitHub Desktop.
Save zachcp/f4cca1f8d4878df2c54eb43659bff472 to your computer and use it in GitHub Desktop.
Molecule in GGPlot Via SVG
library(dplyr)
library(depict)
library(grid)
library(ggplot2)
library(svgparser)
# SVG code to make background box transparent
my_css = '
rect {
fill: none;
fill-opacity: 50%;
}
'
mgrd <- depiction() %>%
depict::set_zoom(3) %>%
depict(parse_smiles("B([C@H](CC(C)C)NC(=O)[C@H](CC1=CC=CC=C1)NC(=O)C2=NC=CN=C2)(O)O"))
img_grob <- svgparser::read_svg(
mgrd$toSvgStr(),
user_css = my_css
)
ggplot(mtcars) +
geom_point(aes(mpg, wt)) +
annotation_custom(img_grob, xmin = 26, xmax = 33, ymin = 4, ymax = 5) +
labs(title = "svgparser::read_svg() + ggplot2") +
theme_bw()
@zachcp
Copy link
Author

zachcp commented Oct 5, 2022

Screen Shot 2022-10-05 at 2 45 00 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment