ggplot2:创建有序的组条形图-(使用重新排序) [英] ggplot2: create ordered group bar plot - (use reorder)
问题描述
我想在保持顺序的同时创建分组的条形图.如果是单列而不是成组的条形图,则显然可以使用重排序功能.但不确定如何在融化的data.frame上使用它.
I want to create grouped bar plot while keeping order. If it was single column and not a grouped bar plot use of reorder function is obvious. But not sure how to use it on a melted data.frame.
这是带有代码示例的详细说明:
Here is the detail explanation with code example:
让我们说我们有以下data.frame:
Lets say we have following data.frame:
d.nfl <- data.frame(Team1=c("Vikings", "Chicago", "GreenBay", "Detroit"), Win=c(20, 13, 9, 12))
在翻转时绘制简单的条形图.
plotting a simple bar plot while flipping it.
ggplot(d.nfl, aes(x = Team1, y=Win)) + geom_bar(aes(fill=Team1), stat="identity") + coord_flip()
以上情节将没有订单,如果我想通过胜利来订购情节,我可以执行以下操作:
above plot will not have an order and if I want to order the plot by win I can do following:
d.nfl$orderedTeam <- reorder(d.nfl$Team1, d.nfl$Win)
ggplot(d.nfl, aes(x = orderedTeam, y=Win)) + geom_bar(aes(fill=orderedTeam), stat="identity") + coord_flip()
现在可以说我们在原始数据框中添加了另一列
Now lets say we add another column (to original data frame)
d.nfl$points <- c(12, 3, 45, 5)
Team1 Win points
1 Vikings 20 12
2 Chicago 13 3
3 GreenBay 9 45
4 Detroit 12 5
要生成分组的条形图,首先我们需要将其融化:
to generate grouped bar plot, first we need to melt it:
library(reshape2)
> d.nfl.melt <- melt(d.nfl[,c('Team1','Win','points')],id.vars = 1)
> ggplot(d.nfl.melt,aes(x = Team1,y = value)) + geom_bar(aes(fill = variable),position = "dodge", stat="identity") + coord_flip()
ggplot上方是无序的.
above ggplot is unordered.
但是我如何排序组栏图(升序)
but how I do ordered group bar plot (ascending manner)
推荐答案
这不是问题.
最简单的方法是不将您所订购的团队丢掉:
The easiest way is to not discard your ordered team in the melt:
d.nfl.melt <- melt(d.nfl,id.vars = c("Team1", "orderedTeam"))
或者,我们可以在融化后使用reorder
,而仅在计算顺序时使用Win
元素:
Alternatively, we can use reorder
after melting and just only use the Win
elements in computing the ordering:
d.nfl.melt$ordered_after_melting = reorder(
d.nfl.melt$Team1,
X = d.nfl.melt$value * (d.nfl.melt$variable == "Win")
)
另一个想法是从原始有序列中获取levels
并将其应用于融化因子:
Yet another idea is to take the levels
from the original ordered column and apply them to a melted factor:
d.nfl.melt$copied_levels = factor(
d.nfl.melt$Team1,
levels = levels(d.nfl$orderedTeam)
)
所有三种方法给出相同的结果. (我省略了coord_flips,因为它们没有添加任何问题,但是您当然可以将它们重新添加.)
All three methods give the same result. (I left out the coord_flips because they don't add anything to the question, but you can of course add them back in.)
gridExtra::grid.arrange(
ggplot(d.nfl.melt,aes(x = orderedTeam, y = value)) +
geom_bar(aes(fill = variable),position = "dodge", stat="identity"),
ggplot(d.nfl.melt,aes(x = ordered_after_melting, y = value)) +
geom_bar(aes(fill = variable),position = "dodge", stat="identity"),
ggplot(d.nfl.melt,aes(x = copied_levels, y = value)) +
geom_bar(aes(fill = variable),position = "dodge", stat="identity")
)
最简单的做法是,我建议在融化时仅将orderedTeam
变量保留在周围.您的代码似乎很难将其遗漏,将其保留很容易.
As to the easiest, I would recommend just keeping the orderedTeam
variable around while melting. Your code seems to work hard to leave it out, it's quite easy to keep it in.
这篇关于ggplot2:创建有序的组条形图-(使用重新排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!