0

I am trying to resolve a simple problem but I did not find a solution. I have a data.frame ordered like this

ID  Description value
Gut-1877    T4_2    574
Gut-1896    T5_2    576
Gut-1466    Donor   544
Gut-1734    T1_2    354
Gut-1720    T0_2    185
Gut-1741    T2_2    173
Gut-1748    T3_2    196
Gut-2431A   T7_2    421
Gut-2125    T6_2    352
Gut-1656    T1_1    258
Gut-1619    T0_1    77
Gut-1323    Relative    351

I want to put this file in another order and I've tried doing this:

data %>% arrange(value) %>%
  mutate(name = factor(Description, levels=c( "Relative","Donor","T0_1","T1_1","T0_2","T1_2","T2_2","T3_2","T4_2","T5_2","T6_2","T7_2")))

But then it did not respect my order as I get:

ID  Description value
Gut-1877    T4_2    574
Gut-1896    T5_2    576
Gut-1466    Donor   544
Gut-1734    T1_2    354
Gut-1720    T0_2    185
Gut-1741    T2_2    173
Gut-1748    T3_2    196
Gut-2431A   T7_2    421
Gut-2125    T6_2    352
Gut-1656    T1_1    258
Gut-1619    T0_1    77
Gut-1323    Relative    351

How can I put it in the order that I would like thus:

"Relative","Donor","T0_1","T1_1","T0_2","T1_2","T2_2","T3_2","T4_2","T5_2","T6_2","T7_2"

And then how do I plot their values following this order? Thanks a lot to everyone will help.

C. Braun
  • 5,061
  • 19
  • 47
Dr.PhilCol
  • 21
  • 5
  • On what basis is the ordering done? – NelsonGon Mar 20 '19 at 15:12
  • Possible duplicate of [Order data frame rows according to vector with specific order](https://stackoverflow.com/questions/11977102/order-data-frame-rows-according-to-vector-with-specific-order) – nghauran Mar 20 '19 at 15:18
  • The data frame doesn't get reordered based on the order of factor levels--you have to call `arrange` or some other function that will put it in order – camille Mar 21 '19 at 15:20

1 Answers1

1

Below is your code, but with arrange added to sort your data frame by Description.

library(dplyr)
library(magrittr)

data <- read.table(text = "ID  Description value
Gut-1877    T4_2    574
           Gut-1896    T5_2    576
           Gut-1466    Donor   544
           Gut-1734    T1_2    354
           Gut-1720    T0_2    185
           Gut-1741    T2_2    173
           Gut-1748    T3_2    196
           Gut-2431A   T7_2    421
           Gut-2125    T6_2    352
           Gut-1656    T1_1    258
           Gut-1619    T0_1    77
           Gut-1323    Relative    351", header = TRUE)

data %<>% arrange(value) %>%
  mutate(Description = factor(Description, levels=c( "Relative","Donor","T0_1","T1_1","T0_2","T1_2","T2_2","T3_2","T4_2","T5_2","T6_2","T7_2"))) %>% 
  arrange(Description)
#>           ID Description value
#> 1   Gut-1323    Relative   351
#> 2   Gut-1466       Donor   544
#> 3   Gut-1619        T0_1    77
#> 4   Gut-1656        T1_1   258
#> 5   Gut-1720        T0_2   185
#> 6   Gut-1734        T1_2   354
#> 7   Gut-1741        T2_2   173
#> 8   Gut-1748        T3_2   196
#> 9   Gut-1877        T4_2   574
#> 10  Gut-1896        T5_2   576
#> 11  Gut-2125        T6_2   352
#> 12 Gut-2431A        T7_2   421

Created on 2019-03-20 by the reprex package (v0.2.1)

You could then plot the data, like so:

ggplot(data, aes(x = Description, y = value)) + geom_bar(stat = "identity")

enter image description here

Dan
  • 11,370
  • 4
  • 43
  • 68
  • thanks a lot this works well!!! but then when I do the plot it comes back to its initial state – Dr.PhilCol Mar 20 '19 at 15:26
  • @Dr.PhilCol Check the updated code. You need to store the updated data frame in `data`. I do this with a compound assignment pipe from `magrittr`. – Dan Mar 20 '19 at 15:27