2017-03-08 28 views
3

年我試圖讓這個有每個隊的線路,與線路匹配圖例中的顏色的顏色。我把這個程序寫成了條形圖,因爲我知道該怎麼做,所以我認爲只需要做一些修改就可以實現。注意:我不想要一條最適合的線,而是一條從點到點連接的線。r多個線圖的MLB隊獲勝的

這接下來的部分可能是非常耗時的,所以我不希望任何一個來解決這個問題,但我也真的很想有圖例中的隊徽,也許更換隊名。然後在圖例中,我想將與團隊相關的顏色作爲一條線而不是一個框。

與一個或兩個的這些將是非常讚賞的任何幫助。

編輯:我想保持所有下面的程序所具有的功能,比如灰色的背景,白色的網格,等。

df <- read.table(textConnection(
    'Year Orioles RedSox Yankees Rays BlueJays 
    1998 79  92  114 63 88 
    1999 78  94  98  69 84 
    2000 74  85  87  69 83 
    2001 63  82  95  62 80 
    2002 67  93  103 55 78 
    2003 71  95  101 63 86 
    2004 78  98  101 70 67 
    2005 74  95  95  67 80 
    2006 70  86  97  61 87 
    2007 69  96  94  66 83 
    2008 68  95  89  97 86 
    2009 64  95  103 84 75 
    2010 66  89  95  96 85 
    2011 69  90  97  91 81 
    2012 93  69  95  90 73 
    2013 85  97  85  92 74 
    2014 96  71  84  77 83 
    2015 81  78  87  80 93 
    2016 89  93  84  68 89'), header = TRUE) 



df %>% 
    gather(Team, Wins, -Year) %>% 
    mutate(Team = factor(Team, c("Orioles", "RedSox", "Yankees","Rays","BlueJays"))) %>% 
    ggplot(aes(x=Year, y=Wins)) + 
    ggtitle("AL East Wins") + 
    ylab("Wins") + 
    geom_col(aes(fill = Team), position = "dodge") + 
    scale_fill_manual(values = c("orange", "red", "blue", "black","purple"))+ 
    theme(
    plot.title = element_text(hjust=0.5), 
    axis.title.y = element_text(angle = 0, vjust = 0.5), 
    panel.background = element_rect(fill = "gray"), 
    panel.grid = element_line(colour = "white") 
) 

回答

4

可以使用geom_path(aes(color = Team))代替geom_col(aes(fill = Team)和一個名爲調色板實現您的基本目標是這樣的:

# break this off the pipeline 
df <- gather(df, Team, Wins, -Year) %>% 
    mutate(Team = factor(Team, c("Orioles", "RedSox", "Yankees","Rays","BlueJays"))) 

# if you want to resuse the same theme a bunch this is nice 
# theme_grey() is the default theme 
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"))) 

# named palettes are easy 
# for specific colors i like hex codes the best 
# i just grabbed these of this nice website TeamColorCodes, could be fun! 
cust <- c("#FC4C00", "#C60C30", "#1C2841", "#79BDEE","#003DA5") 
names(cust) <- levels(df$Team) 

# use geom_path inplace of geom_col 
ggplot(df, aes(x=Year, y=Wins, color = Team)) + 
    geom_path(aes(color = Team)) + 
    scale_color_manual(values = cust) + 
    labs(title = "AL East Wins", 
     subtitle = "Ahhh", 
     y = "Wins", 
     x = "Year") 

Link to teamcolorcodes.com

enter image description here

+0

謝謝您的回答。另外,我怎樣才能讓線條變成淺藍色?我試圖從「藍色」將其更改爲藍色(0.3),但得到一個錯誤 –

+0

此外,一切都看起來不錯,但我想唯一的另一件事是網格線,也許有白色。 –

+0

我肯定會檢查該網站的顏色,謝謝。 –