2017-02-15 109 views
1

我擁有多個地理區域的數據,每個地理區域都有相關的描述和時間序列數據。例如:重新排列多線圖的顏色和圖例,並更改圖例圖標

---in file "data.csv": 
ID,Region,Year,Value 
9,Manhattan,2010,5 
9,Manhattan,2011,6 
10,Brooklyn,2010,6 
10,Brooklyn,2011,7 
11,Bronx,2010,8 
11,Bronx,2011,6 
12,New Jersey,2010,7 
12,New Jersey,2011,5 

(該表由每區一排重塑較早的表形成的,但在這裏,這是不相關的。)我想繪製與ggplot2這個數據和包括ID和描述傳奇。這是我最好的嘗試:

#! /usr/bin/env RSCRIPT 

library(data.table) 
library(ggplot2) 

dt <- fread("data.csv")[,Label:=paste(ID, " (", Region, ")", sep="")] 
png("plot.png") 
gg <- ggplot(data=dt,aes(x=Year,y=Value,group=ID,colour=Label)) + 
    geom_line() + geom_label(aes(label=ID)) 
print(gg) 
dev.off() 

結果:

sample plot

我想提出兩個轉變:

  1. 指定顏色的數值,而不是字母,值—因此「9(曼哈頓)」變紅,「10(布朗克斯)」變爲綠黃色,等等—,同時保持自動顏色變淺TTE。我想避免使用scale_colour_manual()之類的手動顏色選擇。我的實際數據有不同數量的區域,每個圖表最多約20個。

  2. 將圖例中的彩色圖標從小寫字母A更改爲區域ID(如紅色9,綠黃色10等)。這可以讓我單獨使用區域字段作爲圖例文本,而不是「ID(區域)」。

+1

首先,您可以在繪製之前按照所需順序設置要素的級別。 – aosmith

+0

只有通過檢查dt [,ID]或類似的方法才能自動執行該操作嗎? –

+0

如果你只是調用'as.factor(dt $ ID)',它會使你的數據框中的因子級別默認爲order,這對於這種情況看起來已經足夠了,你可能還想看看'library(forcats)'函數易於因子水平 – Nate

回答

1

當前的標籤是因爲9:12字母排序是c("10", "11", "12", "9")。您可以手動更改它,或者你可以使用像mixedsortgtools使用dplyrmagrittr而不是data.table做到這一點,在這裏:

dt %<>% 
    mutate(Label = paste0(dt$ID, " (", dt$Region, ")") %>% 
      factor(levels = mixedsort(unique(.)))) 

更改圖例中的標籤是有點困難,主要是因爲他們有兩個字符(而不是一個)。如果您的標籤都是一個單獨的字符,你可能只是做這樣的事情:

ggplot(data=dt,aes(x=Year,y=Value,group=ID,colour=Label)) + 
    geom_line(show.legend = FALSE) + 
    geom_point() + 
    geom_label(aes(label=ID), show.legend = FALSE) + 
    guides(color = guide_legend(override.aes = list(shape = c("A","B","C","D") 
                , size = 3))) 

enter image description here

但是,你不能(據我所知)的形狀使用多個字符。所以,我訴諸於我的共同失敗:生成我想作爲一個獨立情節並將它們與cowplot拼接在一起的複雜的傳奇。

首先,存儲你想情節沒有傳說

plotPart <- 
    ggplot(data=dt,aes(x=Year,y=Value,group=ID,colour=Label)) + 
    geom_line() + 
    geom_label(aes(label=ID)) + 
    theme(legend.position = "none") 

然後,修改原始數據,以跌停在相同的順序爲每個區域只有一個入口與各區域的因素標籤是(在這裏,使用dplyr,但您可以修改爲使用data.table代替)。將它傳遞給ggplot並生成你想要的佈局。我仍然在左邊的區域,但你可以將它們移動到右邊scale_y_discrete(position = "right")

legendPart <- 
    dt %>% 
    select(ID, Region, Label) %>% 
    filter(!duplicated(.)) %>% 
    arrange(desc(ID)) %>% 
    mutate(Region = factor(Region, levels = Region)) %>% 
    ggplot(
    aes(x = 1 
     , y = Region 
     , color = Label 
     , label = ID)) + 
    geom_label() + 
    theme(legend.position = "none" 
     , axis.title = element_blank() 
     , axis.text.x = element_blank() 
     , axis.ticks.x = element_blank() 
     , panel.grid = element_blank() 
     ) 

然後,加載cowplot。請注意,重置默認theme,所以你需要手動在乘坐它(除非你喜歡的cowplot主題)與theme_set

library(cowplot) 
theme_set(theme_minimal()) 

然後,使用plot_grid縫都在一起。最簡單的版本沒有參數,但不會很大期待:

plot_grid(plotPart, legendPart) 

enter image description here

但是,我們可以控制的間距與rel_widths(你需要發揮它,以適應實際數據和縱橫比):

plot_grid(plotPart 
      , legendPart 
      , rel_widths = c(0.9, 0.2) 
     ) 

給出

enter image description here

我個人喜歡「嘎吱」的傳說了一下,所以我通常窩另一plot_grid調用中的傳說,這裏包括良好的措施標題:

plot_grid(
    plotPart 
    , plot_grid(
    ggdraw() 
    , ggdraw() + draw_label("Legend") 
    , legendPart 
    , ggdraw() 
    , rel_heights = c(1,1,3,2) 
    , ncol = 1 
) 
    , rel_widths = c(0.9, 0.2) 
) 

enter image description here

我相信這符合你的問題的要求,儘管你仍然可能想調整它以符合你的喜好風格等。