0
我正在嘗試更改此網格排列圖的每個方面的標題。我一直在嘗試的每一個標題都會使「大西洋」的標題或錯誤。問題是我需要將一個單詞分成一個單詞,以便它們可以是變量,但我試圖將它們分成兩個單詞作爲實際圖形。我怎麼能使用兩個詞如「SouthWest」改爲「South West」?R更改網格排列中的標題排列
任何幫助將不勝感激。
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)
我想要的「西南」一讀「西南」以及其他兩個冠軍與2個字。有沒有快速解決這個問題?對不起,我應該澄清。我將編輯我的問題。 –
@JohnH我明白了。我更新了答案,爲標籤分配了一個單獨的矢量。 – MrFlick
謝謝。如果我可能會問一個快速無關的問題:你有沒有什麼好的和有效的學習R的建議?剛剛開始學習它。 –