Building heatmap using Qiime2r

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).
:information_source: 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!

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?

Hello Greta,

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

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.

Hello Colin,

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

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

dataframe <- dataframe %>%
  mutate(Abundance = as.numeric(Abundance))
1 Like