按频率/值排序离散 x 比例
我正在使用具有离散 x 刻度的 ggplot 制作闪避条形图,x 轴现在按字母顺序排列,但我需要重新排列它,以便它按 y 轴的值排序(即,最高的条形将位于左侧)。
我尝试了 order 或 sort,但结果是对 x 轴进行排序,而不是对条形进行排序。
我做错了什么?
I am making a dodged bar chart using ggplot with discrete x scale, the x axis are now arranged in alphabetical order, but I need to rearrange it so that it is ordered by the value of the y-axis (i.e., the tallest bar will be positioned on the left).
I tried order or sort, but result in sort the x-axis, but not the bars respectively.
What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对我来说最好的方法是使用带有类别的向量,以便我需要将其作为
scale_x_discrete
的limits
参数。我认为这是非常简单直接的解决方案。The best way for me was using vector with categories in order I need as
limits
parameter toscale_x_discrete
. I think it is pretty simple and straightforward solution.尝试手动设置 x 轴上的因子水平。例如:
正如 James 在他的回答中指出的那样,
reorder
是重新排序因子级别的惯用方法。Try manually setting the levels of the factor on the x-axis. For example:
As James pointed out in his answer,
reorder
is the idiomatic way of reordering factor levels.您可以使用
重新排序
:编辑:
要在左侧设置最高的栏,您必须使用一些拼凑:
我希望它也有负高度,但事实并非如此,所以它有效!
You can use
reorder
:Edit:
To have the tallest bar at the left, you have to use a bit of a kludge:
I would expect this to also have negative heights, but it doesn't, so it works!
Hadley 一直在开发一个名为
forcats
的软件包。这个包使任务变得更加容易。当您想要按因子的频率更改 x 轴的顺序时,可以利用fct_infreq()
。对于本文中的mtcars
示例,您希望根据每个级别的频率对cyl
级别进行重新排序。最常出现的级别位于左侧。您所需要的只是fct_infreq()
。如果您想反其道而行之,可以将
fct_rev()
与fct_infreq()
一起使用。Hadley has been developing a package called
forcats
. This package makes the task so much easier. You can exploitfct_infreq()
when you want to change the order of x-axis by the frequency of a factor. In the case of themtcars
example in this post, you want to reorder levels ofcyl
by the frequency of each level. The level which appears most frequently stays on the left side. All you need is thefct_infreq()
.If you wanna go the other way around, you can use
fct_rev()
along withfct_infreq()
.我意识到这已经很旧了,但也许我创建的这个函数对那里的人有用:
现在,使用这个函数,您可以使用 ggplot2 交互式绘图,如下所示:
可以看出,
order_axis
函数创建另一个数据框,其中有一个名称相同但末尾带有_o
的新列。这个新列的级别按升序排列,因此 ggplot2 会自动按该顺序绘制。这有些限制(仅适用于列的字符或因子和数字组合并按升序排列),但我仍然发现它对于动态绘图非常有用。
I realize this is old, but maybe this function I created is useful to someone out there:
Now, with this function you can interactively plot with ggplot2, like this:
As can be seen, the
order_axis
function creates another dataframe with a new column named the same but with a_o
at the end. This new column has levels in ascending order, so ggplot2 automatically plots in that order.This is somewhat limited (only works for character or factor and numeric combinations of columns and in ascending order) but I still find it very useful for plotting on the go.
另一种选择是使用
forcats
(tidyverse
的一部分)中的fct_relevel
手动设置沿 x 轴的顺序。然而,对于按频率排列,@jazzurro 通过使用fct_infreq
(也来自forcats
)提供了最佳答案。输出
此外,在
ggplot
内使用fct_relevel
之前,该变量需要是一个因子。因此,只需先将factor
应用于变量,然后使用fct_relevel
。输出
Another option is to manually set the order along the x-axis using
fct_relevel
fromforcats
(part oftidyverse
). However, for arranging by frequency, @jazzurro provides the best answer by usingfct_infreq
(also fromforcats
).Output
Further, the variable needs to be a factor before using
fct_relevel
insideggplot
. So, just applyfactor
to the variable first, then usefct_relevel
.Output
如果您事先知道要绘制的级别,@Yuriy Petrovskiy 的答案非常好。如果您不这样做(例如,因为您不想绘制数据中不存在的水平),请考虑使用
limit
函数来指定顺序:来自
scale_x_discrete
的文档:否则,您的图表最终会像这样(可能更好):
@Yuriy Petrovskiy's answer is great if you know the levels you want to plot beforehand. If you don't (e.g. because you don't want to plot levels not present in the data), consider using a
limit
function instead to specify the order:From the documentation of
scale_x_discrete
:Otherwise your graph would end up like this (might be preferable):