Creating a new alphabet for use with gglogo

Heike Hofmann and Eric Hare

2017-02-17

By default, the gglogo package uses polygons created from a Helvetica font to use for creating logo plots. These polygons are stored in the object alphabet.rda, and use the name of the letter or digits they describe as their group identifier:

library(ggplot2)
library(gglogo)
data(alphabet)
head(alphabet)
##           x           y order group pathGroup
## 1 0.1208333 0.004854369     1     A       A.1
## 2 0.1229167 0.007281553     2     A       A.1
## 3 0.4437500 1.000000000     3     A       A.1
## 4 0.5729167 1.000000000     4     A       A.1
## 5 0.8833333 0.000000000     5     A       A.1
## 6 0.7604167 0.000000000     6     A       A.1

Each letter and digit is scaled to an area between 0 and 1 in y direction (which makes the lower case letter ā€˜cā€™ as tall as its upper case counterpart). In x direction the letter scaled proportionally to its extent in y direction.

qplot(x, y, geom="polygon", data=alphabet) + 
  facet_wrap(facets=~group, ncol=13) +
  coord_equal()

In order to change the alphabet used, an alphabet object has to be created, and the link to the object has to be changed so that the logo plots use the correct alphabet in the rendering process.

Creating a new alphabet

The function createPolygons have parameters to choose the font family and the set of letters for which polygons are supposed to be created. The parameters dim and fontsize are only needed, if some letters appear to be chopped off. In that case, the dimension of the image should be increased and/or fontsize should be decreased.

alphas <- c(LETTERS, letters, 0:9)
alphabet_garamond <- createPolygons(alphas, font="Garamond")
alphabet_garamond$group <- factor(alphabet_garamond$group, levels=alphas)

qplot(x,y, geom="polygon", data=alphabet_garamond,  group="pathGroup") +
  facet_wrap(facets=~group, ncol=13)

alphas <- c(LETTERS, letters, 0:9)
alphabet_helvetica <- createPolygons(alphas, font="Helvetica")
alphabet_helvetica$group <- factor(alphabet_helvetica$group, levels=alphas)

qplot(x,y, geom="polygon", data=alphabet_helvetica) +
  facet_wrap(facets=~group, ncol=13)

For use as alphabet in the logo geom, letters have to be scaled to fill the full extent in y-direction so as to fill the surrounding rectangles. Setting the parameter scale to TRUE regulates this.

alphas <- c(LETTERS, letters, 0:9)
alphabet_comic <- createPolygons(alphas, font="Comic Sans MS", scale=TRUE)
alphabet_comic$group <- factor(alphabet_comic$group, levels=alphas)

qplot(x,y, geom="polygon", data=alphabet_comic,  group="pathGroup") +
  facet_wrap(facets=~group, ncol=13)

Logo plot of peptide sequences, first with the unscaled Helvetica alphabet:

data(sequences)
ggplot(data=ggfortify(sequences, seq="peptide", treatment="class")) +
  geom_logo(aes(x = class, y = bits, label=element, group=element, 
                fill = interaction(Polarity, Water)), 
            alphabet = alphabet_braille, 
            alpha = 0.8) +
  facet_wrap(~position) +
  scale_fill_brewer(palette="Paired")