Skip to content

Genealogy Part 4

vrrenske edited this page Oct 18, 2019 · 1 revision

Plot your data using plotTreeBasic & ggtree

To get you started with a tree plot of your cells in a time-lapse, bactMAP contains the plotTreeBasic() function. This function uses ggtree & ggplot2 to build a basic ggplot of a phylogenetic tree. I will guide you through how to use the function, and how to customize your plot.

Note that you can also choose to immediately start building a plot from scratch. If you are already skilled with ggplot2 I’d recommend reading through the ggtree cookbook to see what options are available.

Basic Tree

To build a basic tree, just use plotTreeBasic() as below. Note that when I make a plot, I assign a name to it (in the example below that’s tree1). The plot won’t show in your Rstudio, it will just assign it. Only when you call your plot, like in the second below, it will show:

#make plot
tree1 <- plotTreeBasic(myTreePhylo)

#show plot
tree1

As you see, this produces a tree without any additions. The length of the tree branches corresponds to the time (in frames) of your timelapse movie, but there is no x-axis visible because ggtree standardly uses an “empty” background theme. You can change this yourself. Check the ggplot theme options for more information on how to change parts of a ggplot-theme.

If you have never used ggplot2 before, you will first need to install the package:

#install ggplot2
install.packages("ggplot2")

Now ggplot2is installed, you can load the package. While you only need to install a package once, you will have to re-load a package you want to use in every new R session. The command for loading is library().

library(ggplot2)

I change the x axis this by adding layers on top of my previous plot tree1 using the +-operator. The first layer theme() adds axis text, ticks and gray, dotted gridlines. The second layer xlab() adds a new x-axis title ("Time (frames)):

#add axis numbers (axis.text.x), axis ticks (axis.ticks.x) & gridlines (panel.grid.major.x) in function "theme()"

#add x-axis label using xlab()

tree2 <- tree1 +
  theme(axis.text.x = element_text(),
        axis.ticks.x=element_line(),
        panel.grid.major.x = element_line(color="light grey", linetype="dotted")) +
  xlab("Time (frames)")

#show plot
tree2

Now that looks better. However, I still have no clue which cell is which! To change this, I use ’geom_label()orgeom_text`.

The cell number will show at the point where the given cell will split. If you like the cell numbers to be on the point where it is born instead, change the x-location by subtracting the branch length:

#tree with cell labels at "split-point"
tree3 <- tree2 + geom_label(aes(label=label))

#tree with cell labels at birth
tree4 <- tree2 + geom_label(aes(label=label, x=x-branch.length))

Each extra layout feature can be added on top of the plot I made before as a layer. This works quite nicely, because this way I can make my plot and adjust it part by part.

As you see there are still many things you can change in the appearance to make it look better: the cell labels are very crowded, so you can change the size of the plot vs the size of the labels. Check the ggplot2 Vignette for the wide range of options. More rigorously, you could choose to start again with another ggtree layout in the first place, for instance a circular layout. Here you can find all layout options. In the next sections I will show you some examples as well.


⬅️ Genealogy Part 3: Data Structure ▪️ ◾ ▪️ Genealogy Part 5: Fluorescence Data ➡️
Clone this wiki locally