将多个折线图包装成2x3

时间:2017-03-20 19:35:53

标签: r ggplot2

我希望对此图表进行一些更改。我想:

(1)图例中的线条要厚很多

(2)图表上的线条稍厚(

(3)这个可能很棘手。我想要为所有6个MLB分区制作一个图表,可能是2x3。现在,它只是AL东部数据。有关如何做到这一点的任何想法?我希望整个图表有一个标题,每个图表的子标题表示它的分区。

非常感谢任何这些帮助。

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)


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")+
  guides(color=guide_legend("Team",override.aes=list(size=3)))

1 个答案:

答案 0 :(得分:1)

获得1&amp;的答案2 你可以这样做:

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

enter image description here

请注意,在geom_path调用中我们会在size=之外添加aes()来更改图中所有行的大小。要更改图例中图表的大小,我们可以使用override.aes=list(size=3)

要回答问题的第3部分,我建议使用facet。您应该在数据集上添加另一个代表每个分部的变量,然后添加+ facet_wrap(~division)应该可以得到您想要的内容。

创建假分组变量的一个非常基本的例子是:

df$division<-base::sample(LETTERS[1:6],nrow(df),replace=T)

ggplot(df, aes(x=Year, y=Wins, color = Team)) +
  geom_path(aes(color = Team),size=1) + #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))) #Change size= here to change size of lines in legend

这产生了这个情节:

enter image description here