Order multiple variables in ggplot2

夙愿已清 提交于 2021-02-18 06:27:36

问题


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!


回答1:


@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:




回答2:


To order by decreasing value within each region, we sort by region and then by value within region and then convert airport to a factor with the sorted ordering of the levels. Then, we use faceting to get separate panels for each region.

library(tidyverse)

ggplot(mydf %>% arrange(region, desc(value)) %>%
         mutate(airport=factor(airport, levels=airport)), 
       aes(x=airport,y=value, fill = region)) +
  geom_bar(stat="identity", show.legend=FALSE) +
  geom_text(aes(label=round(value,2), y=0.5*value), colour="white", size=3) +
  facet_grid(. ~ region, scales="free_x", space="free_x") +
  scale_y_continuous(limits=c(-0.005, 1.05*max(mydf$value)), expand=c(0,0)) +
  theme_classic() +
  theme(panel.spacing=unit(0,"pt"), 
        panel.border=element_rect(colour="grey50", fill=NA))




回答3:


Neither of these answers worked for me because I was summarizing data before graphing. After WAY too much time I managed to get it to work. I'm adding generic variables because I don't know how to get it to run on someone elses' data. If anyone wants to replace in the data to make the output actually run with this example be my guest.

Also, you have to round otherwise the mean_se output otherwise you get a massive label.

df%>% 
  group_by(X1, X2) %>% 
 summarize(group = mean_se(Outcome))%>% 
  ggplot(aes(x = X1 %>% fct_reorder(., group$y), y = round(group$y,2) %>% reorder(.,group$y), fill = X2))+
 geom_col(position = position_dodge(0.9)) +
  geom_errorbar(aes(ymin =round(group$ymin,2)%>% reorder(.,group$y) , ymax = round(group$ymax,2)%>% reorder(.,group$y)), width = 0.25, size = 1, position=position_dodge(0.95))


来源:https://stackoverflow.com/questions/43877663/order-multiple-variables-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!