Readme.md

Joyce Robbins September 11, 2024

ggformat

This is a one function addin package. My motivation in writing it was to make it easier to clean up and style ggplot2 code.

Note: this is new – please report bugs or leave comments!

Installation

remotes::install_github("jtr13/ggformat")

Usage

Highlight code thats produce a single ggplot2 graph, run the addin “Format ggplot2 code”, and the code will be cleaned up and styled. For example,

# BEFORE
mtcars |> group_by(cyl) |> summarize(mean_mpg = mean(mpg)) |> ggplot(aes(factor(cyl),
mean_mpg)) + theme_bw(14) + geom_col() + labs(title = "mtcars", x = "number of cylinders", y = "average miles per gallon")

will become:

# AFTER
mtcars |>
  group_by(cyl) |>
  summarize(mean_mpg = mean(mpg)) |>
  ggplot(aes(factor(cyl), mean_mpg)) +
  geom_col() +
  labs(title = "mtcars", x = "number of cylinders", y = "average miles per gallon") +
  theme_bw(14)

What the styled code looks like

Labeling functions (ggtitle, labs, xlab, ylab, annotate) are placed between scales and themes. Guides are added between labeling functions and themes.

Source: https://github.com/rstudio/cheatsheets/raw/master/data-visualization.pdf

The specific sort order is:

##  [1] "%>%"      "|>"       "ggplot"   "geom_"    "stat_"    "coord_"  
##  [7] "facet_"   "scale_"   "xlim"     "ylim"     "ggtitle"  "labs"    
## [13] "xlab"     "ylab"     "annotate" "guides"   "theme_"   "theme"

How it differs from other formatting packages

ggplot() +
    geom_ribbon(data = ribbon, aes(
        ymin = min,
        ymax = max,
        x = x.ribbon,
        fill = 'lightgreen'
    )) +
    geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = 'black')) +
    geom_line(data = data, aes(x = x, y = new.data, color = 'red')) +
    scale_fill_identity(name = 'the fill',
                        guide = 'legend',
                        labels = c('m1')) +
    scale_colour_manual(
        name = 'the colour',
        values = c('black' = 'black', 'red' = 'red'),
        labels = c('c2', 'c1')
    ) +
    xlab('x') +
    ylab('density')

ggformat styles this code as:

ggplot() +
  geom_ribbon(data = ribbon, aes(ymin = min, ymax = max, x = x.ribbon, fill = 'lightgreen')) +
  geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = 'black')) +
  geom_line(data = data, aes(x = x, y = new.data, color = 'red')) +
  scale_fill_identity(name = 'the fill', guide = 'legend', labels = c('m1')) +
  scale_colour_manual(name = 'the colour', values = c('black' = 'black', 'red' = 'red'), labels = c('c2', 'c1')) +
  xlab('x') +
  ylab('density')

Note: I hope to add wrapping so long lines are broken apart after commas:

# NOT YET IMPLEMENTED
ggplot() +
  geom_ribbon(data = ribbon, aes(ymin = min, ymax = max, x = x.ribbon, fill = 'lightgreen')) +
  geom_line(data = ribbon, aes(x = x.ribbon, y = avg, color = 'black')) +
  geom_line(data = data, aes(x = x, y = new.data, color = 'red')) +
  scale_fill_identity(name = 'the fill', guide = 'legend', labels = c('m1')) +
  scale_colour_manual(name = 'the colour', values = c('black' = 'black', 'red' = 'red'),
    labels = c('c2', 'c1')) +
  xlab('x') +
  ylab('density')

Nitty Gritty

Known issues

I welcome your suggestions for improvements.

Acknowledgements

The blog post “strsplit – but keeping the delimiter” by Jakob Gepp was helpful for formulating strsplit() splits that keep the search terms.



jtr13/ggformat documentation built on Sept. 14, 2024, 9:52 a.m.