0

I have a folder with file names as such:

  • 1-EXPERIMENT-CONDITION1 Drug 1
  • 2-EXPERIMENT-CONDITION2 Drug 2
  • ...
  • 10-EXPERIMENT-CONDITION4 Drug 3
  • 11-EXPERIMENT-CONDITION5 Drug 3
  • ...
  • 21-EXPERIMENT-CONDITION1 Drug 2

I have produced a loop for automatic data analysis but the plot part gives me troubles. I cannot force the column names since the loop is built to accept multiple data names as inputs. Once I have produced a bar graph with ggplot2, column names are scrambles in a weird order:

  • 1-EXPERIMENT-CONDITION1 Drug 1
  • 10-EXPERIMENT-CONDITION4 Drug 3
  • 11-EXPERIMENT-CONDITION5 Drug 3
  • ...
  • 2-EXPERIMENT-CONDITION2 Drug 2
  • 21-EXPERIMENT-CONDITION1 Drug 2
  • ...

I tried to order alphabetically or from small to large, but this did not change. I tried to transform values to numeric but this was even worse.

My question is: how can I rearrange column names in ggplot2 to have them alphabetically ordered?

Thanks :)

Mollan
  • 135
  • 1
  • 8

1 Answers1

1

Surprising it may be, but the list is already in alphabetical order. The list is first sorted by the first character - hence all items starting with 1 (1, 10, 19, 199...) come before items starting with 2 (2, 20...).

You have two options:

  • (recommended) Rename your inputs to double digit format by adding 0, i.e. 1-EXPERIMENT to 01-EXPERIMENT and so on
  • Reorder categories after import as described in an excellent answer here
user3599495
  • 107
  • 8