This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to plot in R using ggplot2 in a given order

Hi, Everyone I have data like this

 Phylum  Total
1        Acidobacteria    194

2       Actinobacteria  26302

3      Armatimonadetes     69

4        Bacteroidetes  11617

5          Chloroflexi    349

6        Cyanobacteria   2929

7  Deinococcus-Thermus    289


8           Firmicutes  76614

9       Planctomycetes    384

10      Proteobacteria 148249

11        Spirochaetes   2019

12       Synergistetes    103

13     Verrucomicrobia    609

14       Crenarchaeota    339

15       Euryarchaeota   1812

16      Thaumarchaeota    254

When I am plotting this in R using ggplot2 it's plotting the data in Z-A order.

I want my data to be plotted in the same order as it is given. Anyone, please suggest to me how it can be done?

this is my script

library(reader)

data <- read.csv("C:\\Users\\user\\Documents\\phylum.csv", header=TRUE, stringsAsFact=FALSE)

df = data.frame(data)

sizeRange <- c(0,30)

library(ggplot2)

ggplot(df, aes(x=0,y=Phylum,color=Phylum)) + 
  ggrepel::geom_text_repel(aes(label = Phylum), colour="black", nudge_x = -1.2) + 
  geom_point(aes(size = Total),colour="red",stroke=2) +
  scale_size(range = sizeRange)+ 
  theme_bw() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "white"),
        axis.text.y=element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank())

this is my plot enter image description here

r bubble chart r ggplot2

1 answer

You need to set the factor order for Phylum.

df$Phylum <- factor(df$Phylum, levels=df$Phylum)

Tidyverse alternative

library("tidyverse")

df <- mutate(df, Phylum=fct_inorder(Phylum))

Log in to answer this question.