如何在所有方面订购酒吧? [英] How to order bars within all facets?

查看:106
本文介绍了如何在所有方面订购酒吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是继我(现已删除)

This question is following my (now deleted) question.

这是我的数据:

i <- data.frame(
nbr =c(15.18 ,11.53 ,13.37 ,9.2, 10.9, 12.23 ,9.53, 9.81, 7.86, 12.79,
 22.03 ,17.64 ,18.1, 16.78 ,17.53 ,16.97 ,17.76 ,18.35 ,12.82 ,20.91,
 22.09 ,19.18 ,17.54 ,18.45 ,19.83 ,16.99 ,19.69 ,19.45 ,13.07 ,21.41,
 12.13 ,9.76, 10.79 ,10.74 ,12.43 ,9.65, 12.18 ,11.63 ,6.74, 12.31,
 17.5, 14.75 ,15.2, 13.89 ,15.24 ,17.43 ,15.22 ,14.04,9.49, 15.86,
 8.09, 5.86, 6.68, 7.34, 8.01, 6.35, 8.4, 7.4, 3.88, 6.92 ),
x2=rep(c("a",  "b",   "c",  "d", "e", "f", "g", "h", "i", "j"),6),
s = c(rep(c(rep(c("3"),10),
                    rep(c("4"),10),
                    rep(c("5"),10),
                    rep(c("6"),10),
                    rep(c("7"),10),
                    rep(c("8"),10)),1)))

我想根据我的变量"nbr"以升序从各个方面对钢筋进行排序. 我试过了:

I want to order bars from all my facets in ascending order according to my variable "nbr". I tried :

ii <- i[order(i$s, i$nbr ), ]
sn <- factor(x = 1:60, labels = ii$x2)
ii$sn <- sn
ggplot(data = ii, aes(x = sn , y = nbr)) + 
  facet_wrap(. ~ s, ncol=2,scale='free_x')+   geom_bar(stat = 'identity')+
  theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))

它仅适用于一个构面(第一组= 3),而另一个构面的x轴与第一个构面的x轴具有相同的顺序!

It works only for one facet (the first groupe = "3") and the x-axis of the other facet takes the same order as the x-axis of the first facet !

推荐答案

有关此问题的讨论这里,他们提出了以下两个功能来解决该问题,您可以在此处.

There is a discussion regarding this issue here, which they proposed the below two functions as a solution to this problem which you can find here.

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}


ggplot(ii, aes(reorder_within(sn, nbr, s), nbr)) +
     geom_bar(stat = 'identity') +
     scale_x_reordered() +
     facet_wrap(.~ s, ncol=2,scales = "free_x") + 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=.5,colour='gray50'))

这篇关于如何在所有方面订购酒吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆