Skip to content

Commit 6fe54ca

Browse files
resolves #328
1 parent ad3a5bd commit 6fe54ca

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* When an ordered factor is supplied to the `groups` argument of `compare_groups`, the order of levels is used to arrange the results. This can be used to change the order of groups when the output is used with `heat_tree_matrix` (issue [#323](https://github.com/grunwaldlab/metacoder/issues/323)).
66
* Added `calc_diff_abund_deseq2` function to use DESeq2 to produce output like `compare_group` useful for `heat_tree_matrix`
77
* Fixed error with `primersearch` and `primersearch_raw` when there is a single match to a single query (issue [#326](https://github.com/grunwaldlab/metacoder/issues/326))
8+
* Fixed `Error in grid.Call.graphics(C_setviewport, vp, TRUE) : non-finite location and/or size for viewport` when using `heat_tree_matrix` with only a single comparison. Now a single differential heat tree is made instead of a matrix [#328](https://github.com/grunwaldlab/metacoder/issues/328)).
89

910
## metacoder 0.3.5
1011

R/heat_tree_matrix.R

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,43 @@ heat_tree_matrix <- function(obj, data, label_small_trees = FALSE,
110110

111111
}
112112

113+
# If there is only one comparison, just plot it with a single graph
114+
if (length(treatments) == 2) {
115+
plot <- obj %>%
116+
filter_obs(data,
117+
(treat_1 == treatments[combinations[index, 1]] &
118+
treat_2 == treatments[combinations[index, 2]]) |
119+
(treat_1 == treatments[combinations[index, 2]] &
120+
treat_2 == treatments[combinations[index, 1]])) %>%
121+
metacoder::heat_tree(...)
122+
123+
out_plot <- cowplot::ggdraw() +
124+
cowplot::draw_plot(plot, x = 0, y = 0, width = 1, height = 1) +
125+
cowplot::draw_text(gsub("_", " ", treatments[2]),
126+
x = 0.87, y = 0.8,
127+
size = col_label_size, colour = col_label_color,
128+
hjust = "center", vjust = "bottom") +
129+
cowplot::draw_text("vs",
130+
x = 0.87, y = 0.75,
131+
size = mean(c(row_label_size, col_label_size)),
132+
colour = '#000000',
133+
hjust = "center", vjust = "bottom") +
134+
cowplot::draw_text(gsub("_", " ", treatments[1]),
135+
x = 0.87, y = 0.7,
136+
size = row_label_size, colour = row_label_color,
137+
hjust = "center", vjust = "bottom") +
138+
ggplot2::theme(aspect.ratio = 1)
139+
140+
if (!is.null(output_file)) {
141+
for (path in output_file) {
142+
ggplot2::ggsave(path, out_plot, bg = "transparent", width = 10, height = 10)
143+
}
144+
}
145+
146+
return(out_plot)
147+
}
148+
149+
113150
# Make individual plots
114151
plot_sub_plot <- ifelse(label_small_trees, # This odd thing is used to overwrite options without evaluation
115152
function(..., make_node_legend = FALSE, make_edge_legend = FALSE, output_file = NULL) {
@@ -128,10 +165,10 @@ heat_tree_matrix <- function(obj, data, label_small_trees = FALSE,
128165
set.seed(seed)
129166
obj %>%
130167
filter_obs(data,
131-
(treat_1 == treatments[combinations[index, 1]] &
132-
treat_2 == treatments[combinations[index, 2]]) |
133-
(treat_1 == treatments[combinations[index, 2]] &
134-
treat_2 == treatments[combinations[index, 1]])) %>%
168+
(treat_1 == treatments[combinations[index, 1]] &
169+
treat_2 == treatments[combinations[index, 2]]) |
170+
(treat_1 == treatments[combinations[index, 2]] &
171+
treat_2 == treatments[combinations[index, 1]])) %>%
135172
plot_sub_plot(...) %>%
136173
return()
137174
}
@@ -171,8 +208,12 @@ heat_tree_matrix <- function(obj, data, label_small_trees = FALSE,
171208
named_col <- which(apply(layout_matrix, MARGIN = 2, function(x) all(!is.na(x))))
172209
horz_label_data <- matrix_data[match(layout_matrix[named_row, ], matrix_data$plot_index), ]
173210
vert_label_data <- matrix_data[match(layout_matrix[, named_col], matrix_data$plot_index), ]
174-
subgraph_width <- abs(horz_label_data$x[1] - horz_label_data$x[2])
175-
subgraph_height <- abs(vert_label_data$y[1] - vert_label_data$y[2])
211+
subgraph_width <- ifelse(nrow(horz_label_data) == 1,
212+
key_size,
213+
abs(horz_label_data$x[1] - horz_label_data$x[2]))
214+
subgraph_height <- ifelse(nrow(vert_label_data) == 1,
215+
key_size,
216+
abs(vert_label_data$y[1] - vert_label_data$y[2]))
176217
horz_label_data$label_x <- horz_label_data$x + subgraph_width / 2 # center of label
177218
horz_label_data$label_y <- 0.96 # bottom of label
178219
vert_label_data$label_x <- 0.96 # bottom of rotated label

scratch/issue_328.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
library(metacoder)
2+
3+
x <- parse_tax_data(hmp_otus, class_cols = "lineage", class_sep = ";",
4+
class_key = c(tax_rank = "taxon_rank", tax_name = "taxon_name"),
5+
class_regex = "^(.+)__(.+)$")
6+
7+
meta <- hmp_samples[hmp_samples$body_site %in% c('Nose', 'Throat'), ]
8+
# meta <- hmp_samples
9+
10+
# Convert counts to proportions
11+
x$data$otu_table <- calc_obs_props(x, data = "tax_data", cols = meta$sample_id)
12+
13+
# Get per-taxon counts
14+
x$data$tax_table <- calc_taxon_abund(x, data = "otu_table", cols = meta$sample_id)
15+
16+
# Calculate difference between treatments
17+
x$data$diff_table <- compare_groups(x, data = "tax_table",
18+
cols = meta$sample_id,
19+
groups = meta$body_site)
20+
21+
# Plot results (might take a few minutes)
22+
heat_tree_matrix(x,
23+
data = "diff_table",
24+
node_size = n_obs,
25+
node_label = taxon_names,
26+
node_color = log2_median_ratio,
27+
node_color_range = diverging_palette(),
28+
node_color_trans = "linear",
29+
node_color_interval = c(-3, 3),
30+
edge_color_interval = c(-3, 3),
31+
node_size_axis_label = "Number of OTUs",
32+
node_color_axis_label = "Log2 ratio median proportions")

0 commit comments

Comments
 (0)