2017-03-21 1 views
1

このコードを実行した後、ほとんどすべてを表示するためにスクロールする必要があります。 (コード下図参照)R 2x3グラフ - 色を変更せずに6つのグラフすべてに対して、1つの一般的な凡例から別の凡例に変更する

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 ColorId 
    ALEast  BAL  FC4C00 
    ALEast  BOS  C60C30 
    ALEast  NYY  1C2841 
    ALEast  TBR  79BDEE 
    ALEast  TOR  003DA5 
    ALCentral CHW  000000 
    ALCentral CLE  D50032 
    ALCentral DET  0C2340 
    ALCentral KCR  74B4FA 
    ALCentral MIN  CFAB7A 
    ALWest  HOU  FF7F00 
    ALWest  LAA  BA032E 
    ALWest  OAK  00483A 
    ALWest  SEA  1C8B85 
    ALWest  TEX  003279 
    NLEast  ATL  002855 
    NLEast  MIA  000000 
    NLEast  NYM  002D70 
    NLEast  PHI  BA0C2E 
    NLEast  WSN  BA122B 
    NLCentral CHC  002F6C 
    NLCentral CIN  BA0C2F 
    NLCentral MIL  012143 
    NLCentral PIT  FFC72B 
    NLCentral STL  C41E3A 
    NLWest  ARI  A71930 
    NLWest  COL  24135E 
    NLWest  LAD  002F6C 
    NLWest  SDP  002D62 
    NLWest  SFG  FB5B1F'), header = TRUE) 

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 <- c("#A71930", "#002855", "#FC4C00", "#C60C30","#002F6C", 
      "#000000", "#BA0C2F", "#D50032", "#24135E","#0C2340", 
      "#FF7F00", "#74B4FA", "#BA032E", "#002F6C","#000000", 
      "#012143", "#CFAB7A", "#002D70", "#1C2841","#00483A", 
      "#BA0C2E", "#FFC72B", "#002D62", "#FB5B1F","#1C8B85", 
      "#C41E3A", "#79BDEE", "#003279", "#003DA5","#BA122B") 

ggplot(df3, aes(x=Year, y=Wins, color = Team)) + 
    geom_path(aes(color = Team)) + 
    scale_color_manual(values = cust) + 
    labs(title = "MLB Wins", 
     y = "Wins", 
     x = "Year")+ 
    facet_wrap(~Division, ncol=2, nrow=3) + 
    guides(color=guide_legend("Team",override.aes=list(size=3))) 

Picture

しかし、私は、各部門のために別個の凡例があるようにしたいです。だから私はこのコードを追加しますが、元の色は残っていません。確かに、コードのこの最後のブロックで唯一の変更が必要ですが、変更する必要があるものはわかりません。

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)) + 
    scale_color_manual(values = cust) + 
    labs(title = paste(substr(d, 1, 2), substr(d, 3, nchar(as.character(d)))), 
     y = "Wins", 
     x = "Year") + 
    guides(color=guide_legend("Team",override.aes=list(size=3))) 
}) 

do.call(grid.arrange, mlb_plots) 

enter image description here

+0

[この質問](http://stackoverflow.com/questions/14840542/place-aの可能性のある重複-legend-for-each-facet-wrap-grid-in-ggplot2)または[この質問](http://stackoverflow.com/questions/42937569/r-2x3-graph-changing-from-one-common-legend 6-grap-for-all-legend-for-all-grap)を使用します。 – jesstme

+0

2番目のリンクは私の質問です –

+0

最初の質問は私の質問には答えません。その質問は、私が既にそれをしている間に、各プロットの隣に伝説を置くことに取り組んでいます。 –

答えて

1

例えば、scale_color_manualの名前が付いたベクトルを使用してください:

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)) + 
    scale_color_manual(values = setNames(cust, levels(df3$Team))) + 
    labs(title = paste(substr(d, 1, 2), substr(d, 3, nchar(as.character(d)))), 
     y = "Wins", 
     x = "Year") + 
    guides(color=guide_legend("Team",override.aes=list(size=3))) 
}) 

grid.arrange(grobs=mlb_plots) 
+0

正確に私が望んでいた。ありがとうございます。 –

関連する問題