1

I got the following data set:

Skalen Werte
1 Allgemeine Beanspruchung 1.55
2 Emotionale Beanspruchung 1.59
3 Soziale Beanspruchung 1.79
4 Konflikte/Leistungsdruck 1.76
5 Übermüdung 1.79
6 Energielosigkeit 2.13
7 Somatische Beanspruchung 1.52
8 Erfolg 2.74
9 Soziale Erholung 3.26
10 Somatische Erholung 3.41
11 Allgemeine Erholung 3.84
12 Schlaf 4.29
13 Gestörte Pause 1.07
14 Emotionale Erschöpfung 1.36
15 Verletzungsanfälligkeit 1.59
16 In-Form-sein 3.28
17 Persönliche Verwirklichung 2.42
18 Selbstwirksamkeitsüberzeugung 3.29
19 Selbstregulation 3.41

And I used this code to plot it as a vertical line graph

ggplot(data=df_ebf, aes(x=Skalen, y=Werte,group="")) +
  geom_line() +
  geom_point() +
  coord_flip() 

The result is this: enter image description here

The values belong to the right description on the y-axis. But the order is reverse alphabetically. I want that the order is like it is in the dataset.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
psycho95
  • 131
  • 1
  • 12
  • 1
    Try `ggplot(data=df_ebf, aes(x=Skalen, y=forcats::fct_inorder(Werte), group="")) +` – Jon Spring Mar 04 '21 at 17:27
  • Alternate: https://stackoverflow.com/questions/28391850/reverse-order-of-discrete-y-axis-in-ggplot2 – jsv Mar 04 '21 at 17:28
  • I think you'll find that just about every question on stackoverflow that includes [tag:ggplot2] and mentions *"order of axis labels"* (really just *order*) is resolved with the use of `factor` (or thinking columns were already numeric and were strings). – r2evans Mar 04 '21 at 19:03
  • The answer of @JonSpring helped at least to change the order so that it's not alphabetically ordered anymore. Now it's still reversed, not only the labels but labels and the values have to get reserved – psycho95 Mar 04 '21 at 19:13
  • ok then `forcats::fct_inorder(Werte) %>% forcats::fct_rev` – Jon Spring Mar 04 '21 at 19:20
  • If I just add this to the first command line, it doesn't work – psycho95 Mar 05 '21 at 10:18

1 Answers1

1

I found a different approach to solve this problem. I changed the order of the variables in the dataframe with

df <- df %>%
  map_df(rev)

and then used the first function that Jon Spring suggested in the ggplot command

ggplot(data=df, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
  geom_line() +
  geom_point() +
  coord_flip() 

Now I got the right order in the plot.

Thanks for the support!

psycho95
  • 131
  • 1
  • 12