2015-09-12 19 views
2

我正在繪製Gene1 count(y軸)與樣本類型(x軸)的等級。我希望根據他們的起源組織(乳腺癌,結腸直腸癌,肺癌)以及顏色代碼將樣本類型分組,不管它們是分別來自癌症還是來自正常組織的具有紅色和綠色的顏色。將boxplot數據分組,同時在ggplot2中保留其各自的X軸標籤R

我製作了這個圖表1)帶有FACETS的BOXPLOTS(請參閱下面),這與我的願景非常接近,但顯示了一些主要問題。我來完善對圖形幾個問題:

[IMG] http://i57.tinypic.com/10yfmmw.png[/IMG]

1)每個方面結束了9道(列)從許多沒有被框佔據。我如何去除每個方面未被方塊佔用的通道(列)?

2)我是否可以在不使用小平面的情況下繪製該圖,同時仍然保留如圖所示的分組?

3)是否可以創建兩層facet標籤?即我希望將標籤「Gene1」放置在現有標籤上方和上方。這將使我能夠爲Gene2生成如下圖所示的相同圖形,這樣我就可以在每張圖的頂部使用「主」小平面標籤將兩個圖彼此相鄰。

我希望這是有道理的。感謝大家提出的建議和想法。

請參見下面的代碼,這將使你下載我的數據和重現圖形:

TEST文件導入

fileURL <- "https://dl.dropboxusercontent.com/u/4098921/testfile.csv" 
test <- read.csv(fileURL,header=T) 
head(test) 


> head(test) 
    Subset Tissue  Type id Gene1 Gene2 
1 Normal Breast GTEx_Breast 1 5027 12597 
2 Normal Breast GTEx_Breast 2 5287 12338 
3 Normal Breast GTEx_Breast 3 2385 12543 
4 Normal Breast GTEx_Breast 4 3174 12266 
5 Normal Breast GTEx_Breast 5 6593 11350 
6 Normal Breast GTEx_Breast 6 4648 10932 

1)箱線圖與FACETS

library(ggplot2) 
ggplot(test,aes(x=Type, y=Gene1, fill=Subset))+ 
geom_boxplot(notch=T, notchwidth=0.5,outlier.shape=1,outlier.size=2, coef=1.5)+ 
theme(axis.text=element_text(color="black"))+ 
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.4))+ 
theme(panel.grid.minor=element_blank())+ 
labs(size= "Type",x = "",y = "Rank of Gene1 count", title = "1) BOXPLOT WITH FACETS")+ 
scale_fill_manual(values=c("red","lawngreen"),name="Subset", 
labels=c("Cancer (TCGA)", "Normal (GTEx)"))+ 
facet_grid(~Tissue) 

回答

6

您可以通過添加刪除x軸上未使用的標籤至facet_grid。如果你還加了space = "free",你會得到相同大小的箱子。附:

ggplot(test,aes(x=Type, y=Gene1, fill=Subset))+ 
    geom_boxplot(notch=T, notchwidth=0.5,outlier.shape=1,outlier.size=2, coef=1.5)+ 
    theme(axis.text=element_text(color="black"))+ 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.4))+ 
    theme(panel.grid.minor=element_blank())+ 
    labs(size= "Type",x = "",y = "Rank of Gene1 count", title = "1) BOXPLOT WITH FACETS")+ 
    scale_fill_manual(values=c("red","lawngreen"),name="Subset", 
        labels=c("Cancer (TCGA)", "Normal (GTEx)"))+ 
    facet_grid(~Tissue, scales = "free_x", space = "free") 

你會得到如下圖:

enter image description here

當你不希望使用小但保持分組,您可能希望創建一個新的變量,取分組考慮。你可以做到這一點interaction

# create the new variable 
test$newType <- factor(interaction(test$Tissue,test$Type)) 
# set the correct order of the new variable 
test$newType <- factor(test$newType, 
         levels=levels(test$newType)[order(levels(test$newType))], 
         labels=levels(test$Type)[order(levels(test$newType))]) 

然後你就可以用一個新的情節:

ggplot(test,aes(x=newType, y=Gene1, fill=Subset))+ 
    geom_boxplot(notch=T, notchwidth=0.5,outlier.shape=1,outlier.size=2, coef=1.5)+ 
    theme(axis.text=element_text(color="black"))+ 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.4))+ 
    theme(panel.grid.minor=element_blank())+ 
    labs(size= "Type",x = "",y = "Rank of Gene1 count", title = "1) BOXPLOT WITH FACETS")+ 
    scale_fill_manual(values=c("red","lawngreen"),name="Subset", 
        labels=c("Cancer (TCGA)", "Normal (GTEx)")) 

產生以下情節:

enter image description here

如果你想既包括Gene1Gene2在你的情節中,最好的做法是首先將數據重塑爲長格式:

library(tidyr) 
test2 <- test %>% gather(gene,value,5:6) 

的,你可以做圖用:

ggplot(test2,aes(x=Type, y=value, fill=Subset))+ 
    geom_boxplot(notch=T, notchwidth=0.5,outlier.shape=1,outlier.size=2, coef=1.5)+ 
    theme(axis.text=element_text(color="black"))+ 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.4))+ 
    theme(panel.grid.minor=element_blank())+ 
    labs(size= "Type",x = "",y = "Rank of Gene1 count", title = "1) BOXPLOT WITH FACETS")+ 
    scale_fill_manual(values=c("red","lawngreen"),name="Subset", 
        labels=c("Cancer (TCGA)", "Normal (GTEx)"))+ 
    facet_grid(gene~Tissue, scales = "free_x", space = "free") 

上面的代碼會爲您提供以下情節:

enter image description here

一個選項,以包括不同基因的,而無需使用一個額外的方面層,你可以使用:

ggplot(test2,aes(x=Type, y=value, fill=Subset, alpha=gene))+ 
    geom_boxplot(notch=T, notchwidth=0.5,outlier.shape=1,outlier.size=2, coef=1.5)+ 
    theme(axis.text=element_text(color="black"))+ 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.4))+ 
    theme(panel.grid.minor=element_blank())+ 
    labs(size= "Type",x = "",y = "Rank of Gene count", title = "BOXPLOT WITH FACETS")+ 
    scale_fill_manual(values=c("red","lawngreen"),name="Subset", 
        labels=c("Cancer (TCGA)", "Normal (GTEx)"))+ 
    facet_grid(.~Tissue, scales = "free_x", space = "free") + 
    theme_bw() + 
    theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1)) 

結果:

enter image description here

+0

感謝簡單的解決方案!太棒了。 – Peter

+0

我對交互功能有所瞭解,但令我感到困擾的是X軸標籤變得非常長。但是,該圖使得三個組織難以可視化。有沒有辦法將每個組織的條狀物緊緊地放在一起,並在各個組織類型之間留下很大的間隙? – Peter

+0

@Peter我已經更新了我的答案。 – h3rm4n