2016-12-14 82 views
1

我有兩個變量在多個層次上的基本情節,我可以通過facet_grid()來顯示。因此,我有一組相同顏色的樹皮,按照兩個變量的級別排列。R {ggplot2}在應用的facet_grid()中按顏色定義條形欄?

但是,如果我想表明,我的數據來自不同的來源?即從週五到週日 - 從RED數據,從BLACK數據到週四。

有沒有辦法通過顏色表示我的最終情節,我的數據來自RED和BLACK數據集?

是這樣的:

(例如取自:http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/

enter image description here

示例代碼:

require(ggplot2) 
library(reshape2) 

head(tips) 

ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")+ 
    facet_grid(sex ~ day) 

我知道我可以在Inkscape中eaily改變這些顏色,也許有一個簡單的基於R的解決方案?


數據源(紅色和黑色),我將在圖標題註明:無需指定的情節。

回答

3

通過分配dayfill審美和手動定義爲day你可以在不同值的顏色獲得期望的效果。

下面的代碼:

ggplot(tips, aes(x=total_bill, fill = day)) + geom_histogram(binwidth=2, colour = "white")+ 
    facet_grid(sex ~ day) + 
    scale_fill_manual(values = c("Fri" = "Red", "Sat" = "Red", "Sun" = "Red", "Thur" = "Black")) 

給出瞭如下情節:

enter image description here

傳說可以通過添加+ guides(fill = "none")

隱藏
1

這工作:

ggplot(tips, aes(x=total_bill, fill=day=="Thur")) + geom_histogram(binwidth=2)+ 
    facet_grid(sex ~ day) + 
    scale_fill_manual(values=c("red", "black")) 

編輯:添加顏色「黑」和「紅」

相關問題