ggplot - y 轴的顺序与数据集的顺序不同

问题描述

我得到了以下数据集:

斯卡伦 沃特
1 Allgemeine Beanspruchung 1.55
2 Emotionale Beanspruchung 1.59
3 Soziale Beanspruchung 1.79
4 Konflikte/Leistungsdruck 1.76
5 特殊要求 1.79
6 能量学 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 施拉夫 4.29
13 Gestörte 暂停 1.07
14 Emotionale Erschöpfung 1.36
15 Verletzungsanfälligkeit 1.59
16 形式存在 3.28
17 个人身份验证 2.42
18 Selbstwirksamkeitsüberzeugung 3.29
19 自我调节 3.41

我使用这段代码将其绘制为垂直线图

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

结果是这样的:

enter image description here

这些值属于 y 轴上的正确描述。但顺序是按字母顺序颠倒的。我希望订单就像它在数据集中一样。

解决方法

我找到了一种不同的方法来解决这个问题。 我用

更改了数据框中变量的顺序
df <- df %>%
  map_df(rev)

然后使用 Jon Spring 在 ggplot 命令中建议的第一个函数

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

现在我在情节中得到了正确的顺序。

感谢支持!