joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
586 stars 186 forks source link

Custom x axis labels with plot_bar #1627

Open aravenscraft opened 2 years ago

aravenscraft commented 2 years ago

Hi Joey! Hope you're doing well!

I am trying to make a bar plot where there is on bar per sample (x=samplename), but the x axis labels display "pretty names" with summary information about the samples - in this case, the site name plus the developmental stage of the insect sampled. My sample names are unique but the summary information is not, so there will be some bars that will have identical labels, which means I can't use x=prettyname. I can't figure out how to code the x labels. My attempt below does not work because scale_x_discrete doesn't know to draw prettyname from the phyloseq object.

mgphy.Rdata.zip

plot_bar(mgphy, x='specid', fill='hilo') + theme(axis.text.x = element_text(size=4.5), legend.position="none") + scale_x_discrete(labels ='prettyname')

Any suggestions for how to code this?

gmteunisse commented 2 years ago

Hi aravenscraft,

scale_x_discrete expects labels to be a character vector. You can either extract this from the phyloseq object, or take a two-step approach as below:

p <- plot_bar(mgphy, x='specid', fill='hilo')
p + scale_x_discrete(labels = p$data$prettyname)

Created on 2022-10-20 by the reprex package (v2.0.1)