0

In the below code I want to dodge the geom_bar where asv_numbers is a character variable, LDA is numeric variable, Group variable have four groups (eric, eric /gram, gram, control) on the basis of which I want to dodge the bar plots.

I have tried everything such as moving the x,y variables inside the first aes() function, using geom_col, moving x,y inside geom_bar aes() function, making the asv_numbers factors, using group= Group .

What I am doing wrong

lda_lawn$Group <- factor(lda_lawn$Group)

lda_lawn$ASV_numbers <- factor(lda_lawn$ASV_numbers)

lda_lawn %>%  
ggplot()+aes(y=ASV_numbers)+
  geom_bar(aes(x=LDA,fill=Group, group=Group),stat = 'identity', position='dodge')

Data looks like

structure(list(ASV_numbers = structure(c(7L, 3L, 17L, 6L, 16L, 
13L), levels = c("ASV__1032_k__Bacteria|p__Proteobacteria|c__Alphaproteobacteria|o__Rhodospirillales", 
"ASV__1044_k__Bacteria|p__Acidobacteria|c__Acidobacteriia|o__Solibacterales|f__Solibacteraceae|g__Bryobacter", 
"ASV__105_k__Archaea|p__Thaumarchaeota", "ASV__1152_k__Bacteria|p__Planctomycetes|c__Planctomycetacia|o__Gemmatales|f__Gemmataceae", 
"ASV__1200_k__Bacteria|p__Proteobacteria|c__Deltaproteobacteria|o__RCP-", 
"ASV__131_k__Bacteria|p__Verrucomicrobia|c__Verrucomicrobiae|o__Chthoniobacterales|f__Chthoniobacteraceae|g__Chthoniobacter", 
"ASV__151_k__Bacteria|p__Proteobacteria|c__Gammaproteobacteria|o__WD", 
"ASV__1619_k__Bacteria|p__Planctomycetes|c__Phycisphaerae|o__Tepidisphaerales|f__WD", 
"ASV__1732_k__Bacteria|p__Patescibacteria|c__Parcubacteria|o__Candidatus_Jorgensenbacteria", 
"ASV__1807_k__Bacteria|p__Planctomycetes|c__Planctomycetacia|o__Pirellulales|f__Pirellulaceae", 
"ASV__1874_k__Bacteria|p__Chlamydiae|c__Chlamydiae|o__Chlamydiales|f__Simkaniaceae", 
"ASV__2381_k__Bacteria|p__Actinobacteria|c__Thermoleophilia|o__Solirubrobacterales|f__Solirubrobacteraceae", 
"ASV__267_k__Bacteria|p__Acidobacteria|c__Acidobacteriia|o__Acidobacteriales", 
"ASV__2916_k__Bacteria|p__Armatimonadetes|c__Chthonomonadetes|o__Chthonomonadales|f__Chthonomonadaceae|g__Chthonomonas", 
"ASV__304_k__Bacteria|p__Proteobacteria|c__Alphaproteobacteria|o__Acetobacterales|f__Acetobacteraceae", 
"ASV__318_k__Bacteria|p__Chlamydiae|c__Chlamydiae|o__Chlamydiales|f__cvE", 
"ASV__334_k__Bacteria|p__Acidobacteria|c__Acidobacteriia", "ASV__362_k__Bacteria|p__Actinobacteria|c__Thermoleophilia|o__Solirubrobacterales|f__Solirubrobacteraceae", 
"ASV__370_k__Bacteria|p__Acidobacteria|c__Acidobacteriia|o__Acidobacteriales|f__Acidobacteriaceae|g__Acidipila", 
"ASV__3820_k__Bacteria", "ASV__417_k__Bacteria|p__Acidobacteria|c__Acidobacteriia|o__Acidobacteriales", 
"ASV__521_k__Bacteria|p__Acidobacteria|c__Acidobacteriia", "ASV__532_k__Bacteria|p__Bacteroidetes|c__Bacteroidia|o__Chitinophagales|f__Chitinophagaceae", 
"ASV__594_k__Bacteria|p__Proteobacteria|c__Alphaproteobacteria|o__Acetobacterales|f__Acetobacteraceae", 
"ASV__655_k__Bacteria|p__Planctomycetes|c__Planctomycetacia|o__Pirellulales|f__Pirellulaceae", 
"ASV__803_k__Bacteria|p__Verrucomicrobia|c__Verrucomicrobiae|o__Pedosphaerales|f__Pedosphaeraceae|g__ADurb.Bin-", 
"ASV__870_k__Bacteria|p__Proteobacteria|c__Alphaproteobacteria|o__Caulobacterales|f__Caulobacteraceae", 
"ASV__899_k__Bacteria|p__Proteobacteria|c__Alphaproteobacteria|o__Micropepsales|f__Micropepsaceae", 
"ASV__977_k__Bacteria|p__WPS-", "ASV__987_k__Bacteria|p__Acidobacteria|c__Holophagae|o__Holophagales|f__Holophagaceae|g__Holophaga"
), class = "factor"), Group = structure(c(1L, 3L, 3L, 3L, 2L, 
3L), levels = c("-Eric", "-Eric/-Gram", "-Gram", "Control"), class = "factor"), 
    LDA = c(3.51872903015948, 3.43291135652932, 3.32756545843609, 
    3.238916403443, 3.23512271745982, 3.05070351587495)), row.names = c(NA, 
6L), class = "data.frame")
  • Can you include some example data or reproduce this on a standard data set so others can replicate the issue? – Jon Spring Aug 22 '23 at 14:18
  • Generally, your categorical variable (`ASV_numbers`) would be on the x - axis, but beyond that it's had to help as this does not comprise a minimum reproducible example - can you share some of your dataset or a mocked-up one to work from? – Paul Stafford Allen Aug 22 '23 at 14:19
  • I have added example data now. Please check – شاه نواز Aug 22 '23 at 14:51
  • 4
    You only have one `ASV_numbers` at each point on the axis, so there is nothing to dodge. Dodging would be if you had multiple `ASV_numbers` at each point on the axis that differed by `group`. Then you could dodge by group. It's not clear what you're trying to achieve here. – Allan Cameron Aug 22 '23 at 15:05
  • I want to combine all bar graphs on the basis of groups. such that '-Eric' bars should come on top followed by '-Gram' and so on. Asv numbers can come in any order. – شاه نواز Aug 22 '23 at 17:34

0 Answers0