R Facets为所有6个部门创造2x3的MLB胜利

时间:2017-03-21 00:08:56

标签: r ggplot2 facet

我正在尝试使用facets创建6个图形,以2x3布局,每个不同的MLB部门都有一个图形。我希望整个图形的标题,以及每个图形的标题,表明它是用于哪个分区。我为AL East团队提供了颜色,但这些颜色并没有转化为所有其他部门,所以我怎样才能列出所有团队的颜色?我会继续将它们列在

中吗?
df <- read.table(textConnection(
  'Year ARI ATL BAL BOS CHC CHW CIN CLE COL DET HOU KCR LAA LAD MIA MIL MIN NYM NYY OAK PHI PIT SDP SFG SEA STL TBR TEX TOR WSN
   2016 69  68  89  93  103 78  68  94  75  86  84  81  74  91  79  73  59  87  84  69  71  78  68  87  86  86  68  95  89  95
   2015 79  67  81  78  97  76  64  81  68  74  86  95  85  92  71  68  83  90  87  68  63  98  74  84  76  100 80  88  93  83
   2014 64  79  96  71  73  73  76  85  66  90  70  89  98  94  77  82  70  79  84  88  73  88  77  88  87  90  77  67  83  96
   2013 81  96  85  97  66  63  90  92  74  93  51  86  78  92  62  74  66  74  85  96  73  94  76  76  71  97  92  91  74  86
   2012 81  94  93  69  61  85  97  68  64  88  55  72  89  86  69  83  66  74  95  94  81  79  76  94  75  88  90  93  73  98
   2011 94  89  69  90  71  79  79  80  73  95  56  71  86  82  72  96  63  77  97  74  102 72  71  86  67  90  91  96  81  80
   2010 65  91  66  89  75  88  91  69  83  81  76  67  80  80  80  77  94  79  95  81  97  57  90  92  61  86  96  90  85  69
   2009 70  86  64  95  83  79  78  65  92  86  74  65  97  95  87  80  87  70  103 75  93  62  75  88  85  91  84  87  75  59
   2008 82  72  68  95  97  89  74  81  74  74  86  75  100 84  84  90  88  89  89  75  92  67  63  72  61  86  97  79  86  59
   2007 90  84  69  96  85  72  72  96  90  88  73  69  94  82  71  83  79  88  94  76  89  68  89  71  88  78  66  75  83  73
   2006 76  79  70  86  66  90  80  78  76  95  82  62  89  88  78  75  96  97  97  93  85  67  88  76  78  83  61  80  87  71
   2005 77  90  74  95  79  99  73  93  67  71  89  56  95  71  83  81  83  83  95  88  88  67  82  75  69  100 67  79  80  81
   2004 51  96  78  98  89  83  76  80  68  72  92  58  92  93  83  67  92  71  101 91  86  72  87  91  63  105 70  89  67  67
   2003 84  101 71  95  88  86  69  68  74  43  87  83  77  85  91  68  90  66  101 96  86  75  64  100 93  85  63  71  86  83
   2002 98  101 67  93  67  81  78  74  73  55  84  62  99  92  79  56  94  75  103 103 80  72  66  95  93  97  55  72  78  83
   2001 92  88  63  82  88  83  66  91  73  66  93  65  75  86  76  68  85  82  95  102 86  62  79  90  116 93  62  73  80  68
   2000 85  95  74  85  65  95  85  90  82  79  72  77  82  86  79  73  69  94  87  91  65  69  76  97  91  95  69  71  83  67
   1999 100 103 78  94  67  75  96  97  72  69  97  64  70  77  64  74  63  97  98  87  77  78  74  86  79  75  69  95  84  68
   1998 65  106 79  92  90  80  77  89  77  65  102 72  85  83  54  74  70  88  114 74  75  69  98  89  76  83  63  88  88  65'), header = TRUE)

df2 <- read.table(textConnection(
  'Division     Team
   ALEast       BAL
   ALEast       BOS
   ALEast       NYY
   ALEast       TBR
   ALEast       TOR
   ALCentral    CHW
   ALCentral    CLE  
   ALCentral    DET
   ALCentral    KCR
   ALCentral    MIN
   ALWest       HOU
   ALWest       LAA
   ALWest       OAK
   ALWest       SEA
   ALWest       TEX
   NLEast       ATL
   NLEast       MIA
   NLEast       NYM
   NLEast       PHI
   NLEast       WSN
   NLCentral    CHC
   NLCentral    CIN
   NLCentral    MIL
   NLCentral    PIT
   NLCentral    STL
   NLWest       ARI 
   NLWest       COL
   NLWest       LAD
   NLWest       SDP
   NLWest       SFG'), header = TRUE)

df <- gather(df, Team, Wins, -Year) %>% 
  mutate(Team = factor(Team, c("BAL", "BOS", "NYY","TBR","TOR")))

theme_set(theme_grey() +
            theme(plot.title = element_text(hjust=0.5),
                  axis.title.y = element_text(angle = 0, vjust = 0.5),
                  panel.background = element_rect(fill = "gray"),
                  axis.ticks=element_blank()))


cust <- c("#FC4C00", "#C60C30", "#1C2841", "#79BDEE","#003DA5")
names(cust) <- levels(df$Team)


ggplot(df, aes(x=Year, y=Wins, color = Team)) +
  geom_path(aes(color = Team)) + #Change size= here to change size of lines in graph
  scale_color_manual(values = cust) +
  labs(title = "AL East Wins",
       y = "Wins",
       x = "Year")+
  facet_wrap(~division) +
  guides(color=guide_legend("Team",override.aes=list(size=3)))

或者是否会有6个单独的5种颜色列表?

我已经包含了每个团队的数据以及每个团队所在的部门,然后是我尝试过的计划。

任何帮助都将不胜感激。

SELECT id, datum, ordinary FROM (SELECT *, 0 as `ordinary` FROM 
    `user_todolist` WHERE `done` = '0' 
    AND `deleted` = '0' AND `id_uzivatel` = '1' ORDER BY `datum` ASC) AS a1 
UNION 
SELECT id, datum, ordinary FROM (SELECT *, 1 as `ordinary` FROM 
    `user_todolist` WHERE `done` = '1' AND `deleted` = '0' AND 
    `id_uzivatel` = '1' ORDER BY `datum` DESC) AS a2 
ORDER BY `ordinary`

1 个答案:

答案 0 :(得分:3)

考虑以下数据处理调整:

  1. 通过对列名称进行分项,将mutate()调用中的因素扩展到所有团队。
  2. 为所有6个部门复制rep的颜色矢量。
  3. 重新排列 Division 因子级别,以便它们可以通过AL / NL分割并排显示。
  4. 合并两个数据框,将 Division 作为要传递到facet_wrap的列。
  5. R 代码

    library(tidyr)
    library(ggplot2)
    library(dplyr)
    
    ...
    
    df <- gather(df, Team, Wins, -Year) %>% 
      mutate(Team = factor(Team, names(df)[2:ncol(df)]))
    
    df3 <- merge(df, df2, by="Team")
    df3$Division <- factor(df3$Division, 
                           levels = c("ALEast", "NLEast", "ALCentral", "NLCentral", "ALWest", "NLWest"))
    
    cust <- rep(c("#FC4C00", "#C60C30", "#1C2841", "#79BDEE","#003DA5"), 6)
    

    Facet_Wrap图表

    在数据处理之后,调用facet_wrap ggplot指定 ncol nrow 参数以安排2 X 3布局。

    ggplot(df3, aes(x=Year, y=Wins, color = Team)) +
      geom_path(aes(color = Team)) + #Change size= here to change size of lines in graph
      scale_color_manual(values = cust) +
      labs(title = "AL East Wins",
           y = "Wins",
           x = "Year")+
      facet_wrap(~Division, ncol=2, nrow=3) +
      guides(color=guide_legend("Team",override.aes=list(size=3)))
    

    但是你会注意到,你的团队都拥有与ggplot相同的图例和标题,对整个图表使用相同的图例/标题。

    First Graph Plot Output

    Grid.Arrange图表

    要解决上述问题,请考虑使用gridExtra&#39; grid.arrange布局的多个图表,您可以在其中动态过滤数据框并传递唯一标题:

    library(gridExtra)
    
    mlb_plots <- lapply(c("ALEast", "NLEast", "ALCentral", "NLCentral", "ALWest", "NLWest"), function(d) {
       ggplot(df3[df3$Division==d,], aes(x=Year, y=Wins, color = Team)) +
         geom_path(aes(color = Team)) + #Change size= here to change size of lines in graph
         scale_color_manual(values = cust) +
         labs(title = paste(substr(d, 1, 2), substr(d, 3, nchar(as.character(d)))),  # FOR SPACE BETWEEN AL/NL AND REST
              y = "Wins",
              x = "Year") +
         guides(color=guide_legend("Team",override.aes=list(size=3)))
       })
    
    do.call(grid.arrange, mlb_plots)
    

    Second Graph Plot Output