在ggplot2中排序多个变量 [英] Order multiple variables in ggplot2

查看:49
本文介绍了在ggplot2中排序多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量归入变量并按降序排序.

mydf

 地区机场价值MIA FLL 0.244587909米娅PBI 0.824144687米娅米娅0.484907626纽约EWR 0.731075565纽约LGA 0.708648915纽约市HPN 0.523991258洛杉矶国际机场LGB 0.651847818松下LAX 0.423607479松下国民账户体系0.433837044松下ONT 0.723144957其他MCO 0.657586674其他SJC 0.084138321其他OAK 0.698794154其他BOS 0.85765002其他BNA 0.018953126其他WAS 0.234897245 

我正在尝试复制上面的图.

这是第一次尝试:

  ggplot(mydf,aes(x = airport,y = value,fill = region))+geom_bar(stat ="identity") 

这是第二次尝试:

  ggplot(mydf,aes(x =重新排序(机场,-值,总和),y =值,填充=区域))+geom_bar(stat ="identity") 

我被困在这里.我可以嵌套重新排序吗? reorder(reorder(x,y),y)我不想让此操作成为手动调用每个分组的过程.

  mydf $ order<-c('ONT','LGB','SNA','LAX','PBI','MIA','FLL','EWR','LGA','HPN','BOS','OAK','MCO','WAS','SJC','BNA')ggplot(mydf,aes(x = airport,y = value,fill = region,order = order))+geom_bar(stat ="identity") 

这仍然行不通.我将不胜感激!

解决方案

@ eipi10有一个很好的答案,但是我经常发现自己需要这样做,另外还要考虑其他一些变量,因此使用 forcats 软件包:

  require(dplyr)要求(forcats)mydf%&%;%mutate(排序= -as.numeric(region)+值,机场= fct_reorder(机场,订购,.desc = T))%>%ggplot(aes(机场,值,填充=区域))+ geom_col() 

这是一个示例,说明我可能需要同时使用排序和构面的方法,在其中我添加了 + facet_grid(〜fac,scales ="free_x",space ="free_x")列有我的旅行历史记录的"fac":

I'm attempting to group variables within variables and sort in descending order.

mydf

region  airport value
MIA         FLL 0.244587909
MIA         PBI 0.824144687
MIA         MIA 0.484907626
NYC         EWR 0.731075565
NYC         LGA 0.708648915
NYC         HPN 0.523991258
LAX         LGB 0.651847818
LAX         LAX 0.423607479
LAX         SNA 0.433837044
LAX         ONT 0.723144957
Other   MCO 0.657586674
Other   SJC 0.084138321
Other   OAK 0.698794154
Other   BOS 0.85765002
Other   BNA 0.018953126
Other   WAS 0.234897245

https://i.stack.imgur.com/G1E2k.jpg

I'm trying to reproduce the above graph.

Here is the first attempt:

ggplot(mydf, aes(x=airport,y=value, fill = region)) +  
  geom_bar(stat = "identity")

Here is the 2nd attempt:

ggplot(mydf, aes(x=reorder(airport,-value,sum),y=value, fill = region)) +  
  geom_bar(stat = "identity")

I'm stuck here. Can I nest reorder? reorder(reorder(x, y), y) I'd like not to have to make this a manual process calling out each grouping.

mydf$order <- c('ONT','LGB','SNA','LAX','PBI','MIA','FLL','EWR','LGA','HPN','BOS','OAK','MCO','WAS','SJC','BNA')

ggplot(mydf, aes(x=airport,y=value, fill = region, order = order)) +  
  geom_bar(stat = "identity")

This still doesn't work. I'd appreciate any help!

解决方案

@eipi10 has a great answer, but I often find myself needing to do that, plus facetting on some other variable, so there are other options as well using the forcats package:

require(dplyr)
require(forcats)

mydf %>% 
  mutate(ordering = -as.numeric(region) + value,
         airport = fct_reorder(airport, ordering, .desc = T)) %>% 
  ggplot(aes(airport, value, fill = region)) + geom_col()

Here's an example of how I might need to use both the ordering and the facets, where I add + facet_grid(~fac, scales = "free_x", space = "free_x") with another column named "fac" with my travel history:

这篇关于在ggplot2中排序多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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