# Building heatmap using Qiime2r

**URL:** https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034
**Category:** Other Bioinformatics Tools
**Tags:** qiime2r, heatmap
**Created:** [December 15, 2022, 10:11pm UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034 "2022-12-15T22:11:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gcook207](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gcook207](https://forum.qiime2.org/u/gcook207)
#### Post date: [December 15, 2022, 10:11pm UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034/1 "2022-12-15T22:11:40Z")

</div>

Hello all!

I was wondering if you could help me construct a heat map using qiime2r. I have been using code from some of the other posts on here, but seem to need additional tweaking I cant figure out.

Here is the code I am using:

library(tidyverse)  
library(qiime2R)  
OTUs\<-apply(OTUs.j, 2, function(x) x/sum(x)\*100)  
head(OTUs)  
OTUs\<-  
data.frame(MeanAbundance=rowMeans(OTUs)) %\>%  
rownames\_to\_column("Feature.ID") %\>%  
arrange(desc(MeanAbundance)) %\>%  
top\_n(30, MeanAbundance) %\>%  
pull(Feature.ID)

OTUs %\>%  
as.data.frame() %\>%  
rownames\_to\_column("Feature.ID") %\>%  
gather(-Feature.ID, key="SampleID", value="Abundance") %\>%  
mutate(Feature.ID=if\_else(Feature.ID %in% OTUs, Feature.ID, "Remainder")) %\>%  
group\_by("SampleID", Feature.ID) %\>%  
summarize(Abundance=sum(Abundance)) %\>%  
left\_join(metadata) %\>%  
mutate(NormAbundance=log10(Abundance+0.01)) %\>%  
left\_join(Taxonomy.j) %\>%  
mutate(Feature=paste(Feature.ID, Taxon)) %\>%  
mutate(Feature=gsub("[kpcofgs]\_\_", "", Feature)) %\>%

mutate('Period'=factor('Period',levels = c("A","B","C")))

ggplot(aes(x=`Period`, y=Feature, fill=NormAbundance)) +  
geom\_tile() +  
facet\_grid(~`Donor`, scales="free\_x") +  
theme\_q2r() +  
theme(axis.text.x=element\_text(angle=45, hjust=1)) +  
scale\_fill\_viridis\_c(name="log10(% Abundance)")  
ggsave("qiime2/image/heatmap.pdf", height=4, width=11, device="pdf")

here is the error I am stuck on:

Error in `summarize()`:  
! Problem while computing `Abundance = sum(Abundance)`.  
ℹ The error occurred in group 1: "SampleID" =  
"SampleID", Feature.ID = "Remainder".  
Caused by error in `sum()`:  
! invalid 'type' (character) of argument  
Run `rlang::last_error()` to see where the error occurred.

The OTUs 'type' is listed as a character in R. Does this need to be changed to a data frame ?

Thank you for your time in advance!

---

<div class="post-metadata">

### Author: ![gcook207](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gcook207](https://forum.qiime2.org/u/gcook207)
#### Post date: [December 17, 2022, 6:53am UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034/2 "2022-12-17T06:53:26Z")

</div>

I am new to R and qiime2r plugin. If I have identified the problem correctly, could you help me in figuring out what function in R I need to use and how to identify the correct location in the code to insert the new function?

---

<div class="post-metadata">

### Author: ![colinbrislawn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/colinbrislawn/32/6221_2.png) [@colinbrislawn](https://forum.qiime2.org/u/colinbrislawn)
#### Post date: [December 24, 2022, 3:17am UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034/3 "2022-12-24T03:17:49Z")

</div>

Hello Greta,

Can you run this code and look at the `$Abundance` column?

```R
OTUs %>%
  as.data.frame() %>%
  rownames_to_column("Feature.ID") %>%
  gather(-Feature.ID, key="SampleID", value="Abundance") %>% 
  mutate(Feature.ID=if_else(Feature.ID %in% OTUs, Feature.ID, "Remainder")) %>%
  group_by("SampleID", Feature.ID) %>%
  head()

```

That error is telling us something is a character (a.k.a. string or text) instead of a number, and I'm wondering if the abundance column is the problem.

If it is, we can reformat it as numeric before passing it into the summarize() function.

---

<div class="post-metadata">

### Author: ![gcook207](https://forum.qiime2.org/letter_avatar_proxy/v4/letter/g/cdc98d/32.png) [@gcook207](https://forum.qiime2.org/u/gcook207)
#### Post date: [January 13, 2023, 3:14pm UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034/4 "2023-01-13T15:14:15Z")

</div>

Hello Colin,

Thank you for your reply. I ran your code and yes, the abundance column is listed as character.

---

<div class="post-metadata">

### Author: ![colinbrislawn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/colinbrislawn/32/6221_2.png) [@colinbrislawn](https://forum.qiime2.org/u/colinbrislawn)
#### Post date: [January 24, 2023, 4:47pm UTC](https://forum.qiime2.org/t/building-heatmap-using-qiime2r/25034/5 "2023-01-24T16:47:48Z")

</div>

Sorry to keep you waiting! In case you have not found this yet, you can convert that column from letters to numbers like this:

```auto
dataframe <- dataframe %>%
  mutate(Abundance = as.numeric(Abundance))

```
