如何为这种情况绘制好图

时间:2020-05-04 01:21:52

标签: r ggplot2 plot dplyr

我有此代码:

new_flts.df %>%
  mutate(DELAY_CAT = fct_collapse(DELAY_CAT,
          OnTime = c("On Time", "Short Delay"),
          Delayed = c("Medium Delay", "Long Delay", "Very Long Delay"),
          Canceled =("CNL")
          ))%>%
  filter(DIR=="D") %>%
  group_by(AIRL, ORGDEST, DELAY_CAT)%>%
  tally()%>%mutate(perc = percent(n/sum(n),accuracy = 1))

它产生以下输出(部分输出):

# A tibble: 260 x 5
# Groups:   AIRL, ORGDEST [94]
    AIRL  ORGDEST DELAY_CAT     n perc 
    <fct> <fct>   <fct>     <int> <chr>
  1 ADR   DUS     OnTime       43 49%  
  2 ADR   DUS     Delayed      31 36%  
  3 ADR   DUS     Canceled     13 15%  
  4 ADR   FRA     OnTime     1673 86%  
  5 ADR   FRA     Delayed     237 12%  
  6 ADR   FRA     Canceled     26 1%   
 24 ART   DUS     OnTime       39 57%  
 25 ART   DUS     Delayed      26 38%  
 26 ART   DUS     Canceled      4 6%   
 27 ART   MLH     OnTime       77 53%  
 28 ART   MLH     Delayed      52 36%  
 29 ART   MLH     Canceled     17 12%  
 30 ART   MUC     OnTime       22 61%  
 31 ART   MUC     Delayed      11 31%  
 32 ART   MUC     Canceled      3 8%   
 44 BUR   DUS     OnTime       53 50%  
 45 BUR   DUS     Delayed      45 43%  
 46 BUR   DUS     Canceled      7 7%   
 47 BUR   HAJ     OnTime       17 45%  
 48 BUR   HAJ     Delayed      20 53%  
 49 BUR   HAJ     Canceled      1 3%   
 50 BUR   MLH     OnTime      158 57%  
 51 BUR   MLH     Delayed     101 37%  
 52 BUR   MLH     Canceled     16 6%   
 53 BUR   OTP     OnTime       30 45%  
 54 BUR   OTP     Delayed      30 45%  
 55 BUR   OTP     Canceled      6 9%   

在绘制出最好的图像后,它就是这个(下图)。但是我对此不满意。我想用每个航空​​公司和目的地的性能百分比来显示一个好的图表。任何有助于制作好的信息图的帮助!

地块代码:

ggplot(data, aes(AIRL, perc))+
  geom_col(aes(fill=DELAY_CAT))+facet_wrap(.~ORGDEST, scales = "free")

Plot image

1 个答案:

答案 0 :(得分:0)

我的直觉是,您将从一个地块上获得所有信息而受益,而在这种情况下不使用facet_wrap?您可以使用paste组合您感兴趣的两个变量。看到这个例子。

library(tidyverse)

df <- mtcars

df <- df %>% 
  mutate(CylVs = paste(cyl,vs))

ggplot(df,aes(x = carb,fill = CylVs)) + 
  geom_histogram()

祝你好运!