https://github.com/const-ae/exactplot

Produce scientific figures in R that look exactly how you want

https://github.com/const-ae/exactplot

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Produce scientific figures in R that look exactly how you want

Basic Info
  • Host: GitHub
  • Owner: const-ae
  • License: other
  • Language: R
  • Default Branch: main
  • Size: 550 KB
Statistics
  • Stars: 17
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created over 1 year ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# exactplot




The goal of `exactplot` is to produce millimeter-exact figure layouts and annotate your plots with the full power of Latex. `exactplot` wraps around [`tikzDevice`](https://daqana.github.io/tikzDevice/) and provides utility functions to compose grid plots (e.g., `ggplot2` output, but not base plots).

## Disclaimer

This is a *work-in-progress* package and mainly serves as a collection of scripts that I have used in my past papers to produce my scientific figures. I currently do not plan to release it on CRAN and I am only planning to add features if I need them for my work. If you would like to see some feature included, please open a pull request, or feel free to fork the package.

## Installation

You can install the development version of `exactplot` from Github:

``` r
devtools::install_github("const-ae/exactplot")
```

You might need to install additional fonts. By default, `exactplot` uses the [IBM Plex](https://github.com/IBM/plex) font family. On Mac, you can install them using brew.

``` shell
brew install --cask font-ibm-plex-sans font-ibm-plex-mono font-ibm-plex-math
```

## Example

```{r, eval=FALSE}
library(exactplot)
library(tidyverse)
library(palmerpenguins)
```
```{r, include=FALSE}
devtools::load_all(".")
library(tidyverse)
library(palmerpenguins)
```

The `xp_init` function sets consistent fonts and font sizes, adds additional packages to the `options("tikzLatexPackages")`, and sets the default `ggplot2` theme. The default font sizes are available through the `xp` object. This is needed when you, want to use larger or smaller text in `geom_text()` (remember to change the `size.unit = "pt"`).

```{r}
xp_init()
xp$fontsize
```


Make some simple example plots.

```{r, paged.print=FALSE}
scatter_plot <- penguins |>
  mutate(label = paste0(species, " (", island, ", $", year,"$)")) |>
  ggplot(aes(x = flipper_length_mm, y = bill_length_mm)) +
    geom_point(aes(color = species), show.legend = FALSE) +
    geom_label(data = \(x) slice_max(x, bill_length_mm, by = species), 
               aes(label = label, hjust = ifelse(flipper_length_mm < 200, 0, 1)), vjust = -0.2) +
    labs(x = "Flipper length in $10^{-3}$ m", y = "Bill length in $10^{-3}$ m") +
    coord_cartesian(clip = "off")

beak_lengths <- penguins |>
  ggplot(aes(x = bill_length_mm)) +
   geom_density(aes(fill = species), alpha = 0.7 ) +
   labs(x = "Bill length in $10^{-3}$ m")
```


Combine the two plots with some additional annotations. If you call this function without specifying the filename, you get a quick preview without the latex rendering. By default, `exactplot` uses LuaLatex for rendering, because of its superior font support.

```{r}
xp_compose_plots(
  xp_text("Welcome to \\texttt{exactplot} with \\LaTeX{} support", x = 1, y = 2, 
          fontsize = xp$fontsize_large, fontface = "bold"),
  xp_text("A) Flipper length vs.\\ beak size", x = 1, y = 8),
  xp_plot(scatter_plot, x = 0, y = 12, width = 80, height = 40),
  
  xp_text("B) Beak sizes", x = 84, y = 8),
  xp_plot(beak_lengths, x = 82, y = 12, width = 58, height = 40),
  xp_text("$\\textrm{density}(x) = \\frac{1}{n\\sigma}\\frac{1}{\\sqrt{2\\pi}}\\sum_{i=1}^N{\\exp\\left(\\frac{-(x-x_i)^2}{2\\sigma^2}\\right)}$",
           x = 99, y = 12, fontsize = xp$fontsize_small),
  
  width = 140, height = 52,
  keep_tex_file = FALSE, filename = "man/example.pdf"
)
```



Display the output:

```{r}
print(magick::image_read_pdf("man/example.pdf", density = 600), info = FALSE)
```

# Load additional Latex packages

The `\xleftrightarrow` is defined in the `mathtools` Latex package. You can load it by calling `xp_add_latex_package`.

```{r}
xp_add_latex_package("mathtools")

adv_label_pl <- scatter_plot +
  labs(x = "Smaller $\\xleftrightarrow[\\text{[cm]}]{\\text{\\quad Flipper length\\quad}}$ Larger") +
  theme(axis.title.x = element_text(margin = margin(4, 0, 0, 0, "pt")))

xp_compose_plots(
  xp_plot(adv_label_pl, x = 0, y = 12, width = 80, height = 40),
  
  width = 140, height = 52, filename = "man/example2.pdf"
)
```


Display the output:

```{r}
print(magick::image_read_pdf("man/example2.pdf", density = 600), info = FALSE)
```


# Session Info

```{r}
sessionInfo()
```


Owner

  • Name: Constantin
  • Login: const-ae
  • Kind: user
  • Location: Heidelberg, Germany
  • Company: EMBL

PhD Student, Biostats, R

GitHub Events

Total
  • Watch event: 17
  • Push event: 4
  • Create event: 1
Last Year
  • Watch event: 17
  • Push event: 4
  • Create event: 1

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

DESCRIPTION cran
  • cowplot * imports
  • ggplot2 * imports
  • grid * imports
  • plotgardener * imports
  • purrr * imports
  • scales * imports
  • stringr * imports
  • tikzDevice * imports
  • tinytex * imports
  • withr * imports