0

probably a stupid question. I have a data.frame that looks like:

         Sample3  Sample1  Sample4   Sample5   Sample10
Gene1      6.90    6.45     6.56       3.4       2.0
Gene2      3.4     4.98     0.12       1.5       0.56
Gene3      3.24     0       0.12       0.56      0.22
          .....................

And another data.frame (Sample_ann) that looks like:

Sample   Batch
Sample3  A
Sample1  A
Sample4  B
Sample5  C
Sample10 C
...      ...

I would like to plot Boxplots colouring samples per batch. The point is that ggplot reorder samples but I would like to maintain the order as it is.

here the code:

      mydf_reorder %>%                 
      rownames_to_column("Genes") %>%                                      
      gather(Sample, Sample_value, -Genes) %>%                                        
      left_join(Samples_ann, by = "Sample") %>%                                              
      ggplot(aes(x=Sample, y=Sample_value, color=Batch)) +                                   
      geom_boxplot()                 

I tried to include +geom_bar(stat="identity") but the order is still changed.

Can anyone help me please?

Thank you in advance

NewUsr_stat
  • 2,351
  • 5
  • 28
  • 38

1 Answers1

-1

You need to change the order of the factor in your variables, an example of changing the order of factor in iris dataset:

ggplot(iris,aes(Species))+geom_bar()
levels(iris$Species)# "setosa"     "versicolor" "virginica" 
iris$Species = factor(iris$Species, levels = c(levels(iris$Species[3],levels(iris$Species)[2],levels(iris$Species)[1]))
levels(iris$Species) # "virginica"  "versicolor" "setosa" 
ggplot(iris,aes(Species))+geom_bar()
pari
  • 788
  • 8
  • 12