Plotting Ontological Terms

Daniel Greene

2016-09-09

ontologyPlot lets you to visualise sets of ontological terms as subgraphs of the full ontology. The function provided for doing this is onto_plot. Most of the functions in the package use an ontology_index object, as in the ontologyIndex package, so we start by loading the package, and an example ontology - the Human Phenotype Ontology (HPO).

library(ontologyIndex)
library(ontologyPlot)
data(hpo)

Plotting Terms

A set of terms (character vector of term IDs) can be plotted as a subgraph of the ontology by calling onto_plot:

onto_plot(hpo, terms=c("HP:0001873", "HP:0011877"))

To include all ancestral terms, use the get_ancestors function:

onto_plot(hpo, terms=get_ancestors(hpo, c("HP:0001873", "HP:0011877")))

Terms which just link two terms together can be removed to save space, whilst retaining the structure of the relevant part of the ontology - to do this use remove_links:

onto_plot(hpo, terms=remove_links(hpo, get_ancestors(hpo, c("HP:0001873", "HP:0011877"))))

Node attributes can be passed to onto_plot. Here we pass in a character vector of term IDs to be used as the labels and a character vector of colours generated with rainbow. Note that the length of each vector passed in as an attribute has the same length as terms. However, it is still valid to pass in a vector of length 1, for instance color="blue", because in this case onto_plot recognises this as a request to set given attribute for all nodes to the passed value.

terms <- remove_links(hpo, get_ancestors(hpo, c("HP:0001873", "HP:0011877")))
onto_plot(hpo, terms=terms, label=terms, fillcolor=rainbow(length(terms)))

Functions which automatically select graphical parameters depending on other arguments to onto_plot (i.e. ontology, frequencies and term_sets - see ?onto_plot for further details) can also be passed to onto_plot. For instance, here the labels are set by passing the function official_labels, labelling the nodes by the official names of the corresponding terms.

onto_plot(hpo, terms=terms, label=official_labels)

Any attributes can be passed to onto_plot. Those which are recognised by the package Rgraphviz will be used by plot. Some of the attributes include size (width), shape (shape), font sizes (fontsize) and border colour (color) (see ?onto_plot for details).

Frequencies

A frequencies argument giving a population frequency of (a subset of) terms in the ontology can be passed to the function onto_plot. If this argument is passed, functions which make use of it in determining graphical parameters can be passed in for those parameters, for instance colour_by_population_frequency.

frequencies <- seq(from=0, to=1, by=1/length(terms))
names(frequencies) <- terms
onto_plot(hpo, terms=terms, frequencies=frequencies, 
    fillcolor=colour_by_population_frequency)

Term sets

Another argument which can be passed to onto_plot is term_sets, a list of sets of ontological terms (e.g. a set of HPO encoded phenotypes). If passed, the terms argument of onto_plot defaults to all the terms which help distinguish the cases (see ?remove_uninformative_terms for more details).

hpo_phenotypes <- list(
    A=c("HP:0001382","HP:0004272","HP:0007917","HP:0004912","HP:0001596"),
    B=c("HP:0001382","HP:0004272","HP:0002165","HP:0004800","HP:0004912"),
    C=c("HP:0004800","HP:0001382","HP:0004912","HP:0007917","HP:0008743")
)

onto_plot(hpo, term_sets=hpo_phenotypes, label=label_by_term_set)

To further decorate the plot, we could use the frequency of each term in hpo_phenotypes them to colour them.

onto_plot(
    hpo,    
    frequencies=get_term_frequencies(hpo, hpo_phenotypes),
    term_sets=hpo_phenotypes,
    label=label_by_term_set,
    fillcolor=colour_by_frequency)

Edge attributes

Edge attributes recognised by Rgraphviz can also be supplied by the edge_attributes argument.

onto_plot(
    hpo,    
    frequencies=get_term_frequencies(hpo, hpo_phenotypes),
    term_sets=hpo_phenotypes,
    label=label_by_term_set,
    edge_attributes=list(color="red", lty="dashed"))

Plots for publications

To get the highest quality plots and fine-grained control over the node and edge attributes, the best option is to create the ontological_plot with onto_plot and write it to a file in dot format using the write_dot function. The file can then be opened using a layout program, for example the graphviz program (http://www.graphviz.org/), to visualise/export as an image. Individual node and edge attributes can then be modified by editing the file.