0

I am trying to use facets to create 6 graphs, laid out in a 2x3, with a graph for each different MLB division. I would like there to be a title for the graph as a whole, as well as a title for each graph, indicating which division it is for. I included the colors for the AL East teams, but those colors don't translate to all other divisions, so how could I go about listing the colors for all teams? Would I keep listing them in

cust <- c("#FC4C00", "#C60C30", "#1C2841", "#79BDEE","#003DA5",...,)

or would there be 6 separate lists of 5 colors?

I have included the data for each team as well as which division each team is in, followed by my attempted program.

Any help would be appreciated.

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)))
Remy M
  • 599
  • 1
  • 4
  • 17
  • Note I was asked to post this as a new question because I have new questions. – Remy M Mar 21 '17 at 00:10
  • I assume you are using ggplot2? What kind of plot are you producing -- e.g. barplot, boxplot, etc. You will likely need to reshape your data to long form with division info, and can then set aes fill or colour to team and then set scale_colour_manual or scale_fill_manual to the desired colours/fill values using custom colours. You can do this by factoring the teams and colors in the corresponding order or including the custom colors within the data frame. If you need more help, please post the code you are currently using. – Djork Mar 21 '17 at 00:53
  • line plot. I can provide a picture of what each graph should look like. – Remy M Mar 21 '17 at 00:56
  • Having trouble pasting it. Basically, each graph is a line graph with a line for each team in that respective division, with the x-axis as the year and y-axis as the amount of wins. – Remy M Mar 21 '17 at 01:01
  • I posted the code. You have to scroll down. I posted more than just the data set. – Remy M Mar 21 '17 at 01:02

1 Answers1

3

Consider the following data handling adjustments:

  1. Extend the factors in your mutate() call to all teams by subsetting columns names.
  2. Replicate the colors vector with rep for all 6 divisions.
  3. Reorder your Division factor levels so they can be presented side by side by their AL/NL split.
  4. Merge both two dataframes to have Division as a column to pass in facet_wrap.

R Code

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 Graph

After the data handling, call the facet_wrap ggplot specifying ncol and nrow arguments for arrangement of 2 X 3 layout.

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)))

However you will notice, your teams all share same legend and title as ggplot uses same legend/title for entire graph.

First Graph Plot Output

Grid.Arrange Graphs

To resolve above, consider multiple graphs laid out with gridExtra's grid.arrange where you can dynamically filter the dataframe and pass unique title:

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

Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thank you! If I wanted different colors for different graphs, instead of replicating them, how would I do that? You can use generic colors, such as "red", "blue", ect... just as a guideline. – Remy M Mar 21 '17 at 01:42
  • would I do rep(c(list of 6 different colors), (list of 6 different colors),...,)? – Remy M Mar 21 '17 at 01:43
  • By different graphs, you mean the `grid.arrange` approach, you would pass a list of 6 color vectors and instead of `lapply` use `Map` (wrapper to `mapply`) to iteratively pass a color vector with each *Division* (both lists same length of 6): `Map(function(d,c) { ggplot(...same d...but replace cust with c) }, DivisionList, ColorList)`. – Parfait Mar 21 '17 at 12:11
  • Ok, I'll try that. Any idea why my y-axis label is written vertically and my titles for each graph aren't centered? I posted the same blocks of codes as you provided. – Remy M Mar 21 '17 at 13:42
  • I'm still having trouble. I added a variable called colorID to df2 listing all the color codes I would like to use. Shall I edit that into my question? – Remy M Mar 21 '17 at 15:51
  • Then, after merging, df3 should have a new column `colorID`. If so, add this line just before the ggplot() call still inside `lapply`'s function: `cust <- df3[df3$Division==d, c("colorID")]`. This will reset *cust* with each datafame, used in `ggplot`, filtering only the *colorIDs* for the six Division teams. – Parfait Mar 21 '17 at 23:54