I am working with a ggtree
plot. Data used is available here and you can pull the phylo tree directly from ggtree
.
Load required packages
#load libraries
pacman::p_load(
here,
dplyr,
googlesheets4,
tidyverse,
rotl,
dplyr,
ape,
ggplot2,
ggnewscale,
ggtree,
ggtreeExtra,
taxize,
rentrez
)
Import data
data_for_tree_plot <- read.csv(here("data", "data_for_tree_plot.csv")) #data is available through link above
nwk <- system.file("extdata", "sample.nwk", package="treeio")
mytree <- groupClade(read.tree(nwk), c(17, 21))
Build phylogenetic tree
p <- ggtree(mytree, layout = "circular", lwd = 0.75, aes(colour=species_class))
p <- p %<+% data_for_tree_plot + # link plot to data
scale_colour_manual(values = c("#EE3377", "#332288", "#EECC66", "#6699CC", "#EE7733", "#882255"), #colours for taxa
name="Class",
breaks=c( "Arachnida", "Insecta", "Actinopterygii", "Mammalia", "Reptilia", "Aves"))
p # Circular tree
This is fine. However, I would like to change the legend key so that it looks like this instead.
I have tried changing the legend.key
fill, but this changes all the fills instead of one fill at the time. I have also tried theguide_legend(override.aes
, but this also does nothing.
Does anyone know how to change the legend key shapes for ggtree objects?
If you have an up-to-date version of ggplot
, you can use the key_glyph
parameter to change the glyph.
Using some example data from ggtree
(sorry, I'm not going to download all that):
library(ggtree)
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree2 <- groupClade(read.tree(nwk), c(17, 21))
ggtree(tree2, aes(color=group), key_glyph = 'rect')
Now, normally, we would pass key_glyph = "rect"
to the relevant geom
(and we could do that here with geom_tree(key_glyph = 'rect')
), but it seems that ggtree
passes any ...
arguments to the geom (the documentation doesn't specify this, ggtree
can be a bit odd at times).