knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.retina = 2, fig.width = 6, fig.height = 4, fig.align = "center" )
library(ggplot2) library(MKHthemes) library(tibble)
ggplot2 is awesome!
However, some of its defaults are ... undesirable and ugly.
MKHthemes
provides ways to improve ggplot2 graphs.
By default, MKHthemes
provides more pleasing graphs
than ggplot2, and
it includes a few options to tweak graphs easily.
The package contains only one function, xy_theme()
.
By default, xy_theme()
looks great.
Arguments provide easy ways to adjust commonly needed but esoteric settings
that are hidden deep within ggplot2.
This vignette describes the function xy_theme()
and its arguments.
Here is a data frame for the examples that follow. It contains descenders for facet labels, to ensure the labels are positioned correctly.
df <- tibble::tribble(~x, ~y, ~EorX, ~Country, 1, 2, "Energy", "Georgia", 2, 3, "Energy", "Georgia", 4, 5, "Exergy", "Georgia", 6, 7, "Exergy", "Georgia", 11, 12, "Energy", "Fiji", 12, 13, "Energy", "Fiji", 14, 15, "Exergy", "Fiji", 16, 17, "Exergy", "Fiji") df
The default settings for ggplot2 give the following graph.
p <- df |> ggplot2::ggplot(mapping = ggplot2::aes(x = x, y = y, colour = Country)) + ggplot2::geom_line() + ggplot2::facet_grid(rows = ggplot2::vars(Country), cols = ggplot2::vars(EorX)) p
I find the figure quite ugly:
To improve the appearance of the graph,
use xy_theme()
.
p + MKHthemes::xy_theme()
By default, xy_theme()
xy_theme()
provides several arguments
that enable easy adjustments of graphical parameters,
as shown in the following examples.
# Change font size. Default is 12 points. p + MKHthemes::xy_theme(font_size = 8) # Change font family. Default is "", a sans-serif font. p + MKHthemes::xy_theme(font_family = "Times New Roman") # Change size of all non-axis-title fonts. p + MKHthemes::xy_theme(font_size_scale = 0.4) # Adjust tick length in points. Default is -0.3*font_size. # Negative values are strongly recommended, # because they point the ticks inward. p + MKHthemes::xy_theme(tick_length = -7) # Adjust spacing between axis values and the border in points. # Defaults are 0.2*font_size. p + MKHthemes::xy_theme(x_axis_labels_spacing = -5, y_axis_labels_spacing = 20) # Change facet label distance from the graph in points. # Defaults are 0.2*font_size. p + MKHthemes::xy_theme(col_facet_labels_spacing = -3, row_facet_labels_spacing = 20) # Change border and label colour. Default is "gray50". p + MKHthemes::xy_theme(border_and_label_colour = "red")
MKHthemes
and microtype
Note that xy_theme()
scales fonts to arbitrary precision.
If you use MKHthemes
with knitr
within $\LaTeX$,
you may need to disable font scaling by the microtype
package
to avoid errors on Windows machines.
We have found the following code helpful.
\usepackage{microtype} \microtypesetup{expansion=false}
MKHthemes
provides pleasing ggplot2 graphics,
with sensible defaults and easy adjustment
to otherwise-esoteric ggplot2 settings.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.