Adapting heat trees for non-taxonomic analyses

69 views
Skip to first unread message

msis...@gmail.com

unread,
Jun 5, 2019, 2:46:03 PM6/5/19
to metacoder
Hello!

I stumbled upon the metacoder package and the beautiful heat tree plots included in this package, and I was wondering if these have been adapted for anything besides taxonomic analyses? I am a fish ecologist working on a project where we are looking at the influence of fishing mortality on age structure of spawning stocks across periods of high and low exploitation (number of age classes and age diversity), and this type of plot would be perfect to visualize our analysis. I am finding it quite difficult to adapt the code to match this kind of data, largely because of the taxmap class obj used (which I am unfamiliar with). Essentially I would want each branch/node to be a year, with stems representing each age class (size of which is determined by count of individuals within that age class). Do you have any suggestions on how to do this? Other tree plots I've found in R are not nearly as aesthetically pleasing as this one, so I would love to use this package.

Cheers,
Matt

Zachary Foster

unread,
Jun 5, 2019, 4:43:56 PM6/5/19
to metacoder
Hi Matt,

Yea, you can plot any hierarchical data. Just think "category" when you see "taxon". Im not sure how your data is structured, but below is an example based on my best guess. Try it out and let me know if you have questions. If you have problems adapting your data, I can help if you send me your data or a subset with the same format.

Best,

Zach

library(metacoder)

# Make fake data
fish_data <- data.frame(stringsAsFactors = FALSE,
                        root = "data", # needed to connect years
                        year = rep(2012:2018, each = 3),
                        age = rep(c("fry", "adult", "old"), 7),
                        n = round(rnorm(21, mean = 100, sd = 40)))
fish_data

# Convert to taxmap format
fish_tm <- parse_tax_data(fish_data, class_cols = c("root", "year", "age"))
fish_tm

# Sum abundance per category
fish_tm$data$cat_counts <- calc_taxon_abund(fish_tm, data = "tax_data", cols = "n", out_names = "count")
fish_tm

# Plot
heat_tree(fish_tm,
          node_size = count,
          node_color = count,
          node_label = taxon_names)

msis...@gmail.com

unread,
Jun 7, 2019, 3:39:45 PM6/7/19
to metacoder
Hi Zach,

Thanks very much for your help! I was able to make some plots (attached) which show the age structure data across periods and age classes (1st plot), and the age diversity index across periods and years (H; 2nd plot). The only thing I wish I could change is the color gradient used. Since high count colors are only used in the roots it kind of obscures the differences in the leaves (age classes or years), which are significant at least for age diversity. Is there a way to shift the gradient so that it only considers data from a certain node out? 

Thanks again,
Mattt


wf.age.heattree.pngwf.H.heattree.png


Zachary Foster

unread,
Jun 7, 2019, 7:36:36 PM6/7/19
to metacoder
Hi Matt,

Nice! Yea, you can use the `node_color_interval` option for that. Set the max value (the second one) to something around the maximum value you care about. You can also log transform the color scale using `node_color_trans`. The other option is to define a new color scale with more colors. However, the default one is probably one of the best color-blind friendly scale you will find (viridis).

Unrelated to the color, if you want to reduce the size of that middle node, you could use `node_size_range` to force the codes to be a specific size, in terms of proportion of graph space.

Below are some examples.

Best,

Zach

library(metacoder)

# Make fake data
fish_data <- data.frame(stringsAsFactors = FALSE,
                        root = "data", # needed to connect years
                        year = rep(2012:2018, each = 3),
                        age = rep(c("fry", "adult", "old"), 7),
                        n = round(rnorm(21, mean = 100, sd = 40)))
fish_data

# Convert to taxmap format
fish_tm <- parse_tax_data(fish_data, class_cols = c("root", "year", "age"))
fish_tm

# Sum abundance per category
fish_tm$data$cat_counts <- calc_taxon_abund(fish_tm, data = "tax_data", cols = "n", out_names = "count")
fish_tm

# Plot

heat_tree(fish_tm,
          node_size = count,
          node_color = count,
          node_label = taxon_names)

heat_tree(fish_tm,
          node_size = count,
          node_color = count,
          node_label = taxon_names,
          node_size_range = c(0.03, 0.13),
          node_color_interval = c(min(count), 300))


heat_tree(fish_tm,
          node_size = count,
          node_color = count,
          node_label = taxon_names,
          node_size_range = c(0.03, 0.13),
          node_color_trans = "log10")


heat_tree(fish_tm,
          node_size = count,
          node_color = count,
          node_label = taxon_names,
          node_size_range = c(0.03, 0.13),
          node_color_range = c("red", "green", "blue", "yellow", "magenta"),
          node_color_trans = "log10")
Reply all
Reply to author
Forward
0 new messages