Trim X axis Labels on Heatmap

Hi! I'm pretty new to R, and am having trouble editing and visualizing a heatmap I've made. I used the following code to create the heatmap you can see below:

metadata<-read_q2metadata("Qiime_16S_merged copy.tsv")

SVs<-apply(SVs, 2, function(x) x/sum(x)*100) #convert to percent

data.frame(MeanAbundance=rowMeans(SVs)) %>% #find the average abundance of a SV
rownames_to_column("Feature.ID") %>%
arrange(desc(MeanAbundance)) %>%
top_n(10, MeanAbundance) %>%
pull(Feature.ID) #extract only the names from the table

SVs %>% %>%
rownames_to_column("Feature.ID") %>%
gather(-Feature.ID, key="SampleID", value="Abundance") %>%
mutate(Feature.ID=if_else(Feature.ID %in% SVsToPlot, Feature.ID, "Remainder")) %>% #flag features to be collapsed
group_by(SampleID, Feature.ID) %>%
summarize(Abundance=sum(Abundance)) %>%
left_join(metadata) %>%
mutate(NormAbundance=log10(Abundance+0.01)) %>% # do a log10 transformation after adding a 0.01% pseudocount. Could also add 1 read before transformation to percent
left_join(taxonomy) %>%
mutate(Feature=paste(Feature.ID, Taxon)) %>%
mutate(Feature=gsub("[kpcofgs]__", "", Feature)) %>% # trim out leading text from taxonomy string
ggplot(aes(x=SampleID, y=Feature, fill=NormAbundance)) +
geom_tile() +
facet_grid(~Depth, scales="free_x") +
theme_q2r() +
theme(axis.text.x=element_text(angle=45, hjust=1)) +
scale_fill_viridis_c(name="log10(% Abundance)")
ggsave("heatmap.pdf", height=4, width=11, device="pdf") # save a PDF 3 inches by 4 inches

results<-results %>% mutate(Significant=if_else(we.eBH<0.1,"*", ""))

tree<-drop.tip(tree, tree$tip.label[!tree$tip.label %in% results$Feature.ID]) # remove all the features from the tree we do not have data for
ggtree(tree, layout="circular") %<+% results +
geom_tippoint(aes(fill=diff.btw), shape=21, color="grey50") +
geom_tiplab2(aes(label=Significant), size=10) +
scale_fill_gradient2(low="darkblue",high="darkred", midpoint = 0, mid="white", name="log2(fold-change") +
ggsave("tree.pdf", height=10, width=10, device="pdf", useDingbats=F)

However, the long names of the x axis titles is making it hard to view the data. Is it possible to write a line of code to shorten the x axis labels? I'd like to remove the OTU labels (the string of characters at the beginning). Let me know if that's possible! The line "mutate(Feature=gsub("[kpcofgs]__", "", Feature)) %>% # trim out leading text from taxonomy string" is supposed to shorten the axis titles but does not seem to filtering everyting.

Hard to help without knowing what the data looks like. You might get more help by providing a reprex.

The line "mutate(Feature=gsub("[kpcofgs]__", "", Feature)) %>% # trim out leading text from taxonomy string" is supposed to shorten the axis titles but does not seem to filtering everyting.

Indeed, that could work. Let's dive in: mutate(Feature = xxx(Feature)) means you are replacing the column Feature by a modified version (as defined by xxx). Here you are using gsub() which will recognize a regular expression (regex) and replace it. Regex are a very powerful tool to match any string, but can get very confusing.

This one says to remove a string containing the character k OR p OR c OR ... OR g OR s, if it is followed by two underscores. Based on your screen cap that's not what the OTU looks like. The motif you are testing for is any number of lowercase letters and numbers, followed by a space, then the letter d, then two underscores. The corresponding regex is: "[a-z0-9]+ d__" (the + indicates any number of characters).

In the most extreme case, you could try selecting only the last taxon of the list using this function:

select_last <- function(s){
  s_split <- str_split(s, "; ")
  map_chr(s_split, ~ .x[length(.x)])

that you can apply in the same way:

mutate(Feature = select_last(Feature))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.