This is an R package to apply my custom theme on ggplot2 charts.
There are two functions:
dfr_theme()
to modify the theme components; and
dfr_convert_font_size()
to convert font size from points (pts) to
millimeters (mm).
if (!require("devtools")) install_packages("devtools")
library(devtools)
devtools::install_github("dzulfiqarfr/dfrtheme")
library(dplyr)
library(ggplot2)
library(ggtext)
library(gapminder)
library(dfrtheme)
gapminder_latest <- dplyr::filter(gapminder, year == last(year))
ggplot(
gapminder_latest,
aes(x = gdpPercap, y = lifeExp, fill = continent)
) +
geom_point(
pch = 21,
color = "white",
size = 2.5,
stroke = 0.25,
alpha = 0.75
) +
geom_text(
aes(label = country),
size = dfr_convert_font_size(),
check_overlap = TRUE,
hjust = "inward",
vjust = "inward"
) +
scale_x_log10() +
labs(
title = "Countries with higher income have better public health",
subtitle = paste0(
"GDP per capita at 2005 international dollars ",
"and life expectancy in 2007"
),
x = "GDP per capita<br>(log scale)",
y = "Life expectancy<br>(years)",
caption = "Source: Gapminder<br>Chart: Dzulfiqar Fathur Rahman"
) +
dfr_theme()
Best practices to create a new ggplot2 theme (read this article)
Understanding text size and resolution in ggplot2 by Christophe Nicault (read his blog post)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.