R 在网格排列中更改构面的标题

R Changing Titles For Facets In Grid Arrange

我正在尝试更改此网格排列图的每个方面的标题。我一直在尝试的所有内容要么使每个标题 "Atlantic" 要么给出错误。问题是我需要用一个词来做一些划分,这样它们就可以成为变量,但我试图把它们变成两个词来表示实际的图形。我怎样才能让使用两个词的标题如 "SouthWest" 读作 "South West"?

如有任何帮助,我们将不胜感激。

df <- read.table(textConnection(
'Year ATL BOS   BRK CHI CHO CLE DAL DEN DET GSW HOU IND LAC LAL MEM MIA MIL MIN NOP NYK OKC ORL PHI PHO POR SAC SAS TOR UTA WAS 
2017 42 52  20  40  36  51  32  38  37  66  54  41  50  25  43  40  42  31  33  30  46  28  28  24  41  31  61  50  50  49  
2016 48 48  21  42  48  57  42  33  44  73  41  45  53  17  42  48  33  29  30  32  55  35  10  23  44  33  67  56  40  41  
2015 60 40  38  50  33  53  50  30  32  67  56  38  56  21  55  37  41  16  45  17  45  25  18  39  51  29  55  49  38  46  
2014 38 25  44  48  43  33  49  36  29  51  54  56  57  27  50  54  15  40  34  37  59  23  19  48  54  28  62  48  25  44  
2013 44 41  49  45  21  24  41  57  29  47  45  49  56  45  56  66  38  31  27  54  60  20  34  25  33  28  58  34  43  29  
2012 40 39  22  50  7     21    36  38  25  23  34  42  40  41  41  46  31  26  21  36  47  37  35  33  28  22  50  23  36  20  
2011 44 56  24  62  34  19  57  50  30  36  43  37  32  57  46  58  35  17  46  42  55  52  41  40  48  24  61  22  39  23  
2010 53 50  12  41  44  61  55  53  27  26  42  32  29  57  40  47  46  15  37  29  50  59  27  54  50  25  50  40  53  26  
2009 47 62  34  41  35  66  50  54  39  29  53  36  19  65  24  43  34  24  49  32  23  59  41  46  54  17  54  33  48  19  
2008 37 66  34  33  32  45  51  50  59  48  55  36  23  57  22  15  26  22  56  23  20  52  40  55  41  38  56  41  54  43  
2007 30 24  41  49  33  50  67  45  53  42  52  35  40  42  22  44  28  32  39  33  31  40  35  61  32  33  58  47  51  41  
2006 26 33  49  41  26  50  60  44  64  34  34  41  47  45  49  52  40  33  38  23  35  36  38  54  21  44  63  27  41  42  
2005 13 45  42  47  18  42  58  49  54  34  51  44  37  34  45  59  30  44  18  33  52  36  43  62  27  50  59  33  26  45'), header = TRUE)

df2 <- read.table(textConnection(
'Division   Team 
SouthEast   ATL
Atlantic    BOS
Atlantic    BRK
Central   CHI
SouthEast   CHO
Central   CLE
SouthWest   DAL
NorthWest   DEN
Central   DET
Pacific   GSW
SouthWest   HOU
Central   IND
Pacific   LAC
Pacific     LAL
SouthWest   MEM
SouthEast   MIA
Central   MIL
NorthWest   MIN
SouthWest   NOP
Atlantic    NYK
NorthWest   OKC
SouthEast   ORL
Atlantic    PHI
Pacific   PHO
NorthWest   POR
Pacific   SAC
SouthWest   SAS
Atlantic    TOR
NorthWest   UTA
SouthEast   WAS'), 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("Atlantic", "Central", "SouthEast", "NorthWest", "Pacific", "SouthWest"))

cust <- c("#E03A3E","#008348","#000000","#CE1141","#008CA8",
          "#860038","#007DC5","#4FA8FF","#001F70","#006BB6",
          "#CE1141","#FFC633","#ED174C","#FDB927","#6189B9",
          "#98002E","#00471B","#005083","#B4975A","#F58426",
          "#FDBB30","#007DC5","#006BB6","#E56020","#F0163A",
          "#724C9F","#000000","#CE1141","#00471B","#002566")

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

library(gridExtra)

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


nba_plots <- lapply(c("Atlantic", "Central", "SouthEast", "NorthWest", "Pacific", "SouthWest"), function(d) {
ggplot(df3[df3$Division==d,], aes(x=Year, y=Wins, color = Team))+
theme(panel.background = element_rect(fill = "gray"))+
geom_path(aes(color = Team),size=1.23) +
scale_color_manual(values = setNames(cust, levels(df3$Team))) +
labs(title=c("Atlantic", "Central", "South East", "North West", "Pacific", "South West"),
y = "Wins",
x = "Year") +
guides(color=guide_legend("Team",override.aes=list(size=3)))
})

grid.arrange(grobs=nba_plots)

如果您想分配单独的名称,也只需使用 `Map()~

遍历它们
divisions <- c("Atlantic", "Central", "SouthEast", "NorthWest", "Pacific", "SouthWest")
division_names <- c("Atlantic", "Central", "South East", "North West", "Pacific", "South West")
div_draw <- function(d, dn) {
  ggplot(df3[df3$Division==d,], aes(x=Year, y=Wins, color = Team))+
  theme(panel.background = element_rect(fill = "gray"))+
  geom_path(aes(color = Team),size=1.23) +
  scale_color_manual(values = setNames(cust, levels(df3$Team))) +
  labs(title=dn, y = "Wins",x = "Year") +
  guides(color=guide_legend("Team",override.aes=list(size=3)))
}
nba_plots <- Map(div_draw, divisions, division_names)

因为 d 包含当前部门的名称。这将给出以下情节