2016-08-04 78 views
1

我有一個分組條形圖下面輸入我與GGPLOT2生成:řGGPLOT2:顏色分組基於條件/數值閾值barplot

Disease;Category;Value;pValue 
Disease A;Count;100;0.0001 
Disease A;Expected Count;50;0.0001 
Disease B;Count;80;0.0005 
Disease B;Expected Count;40;0.0005 
Disease C;Count;60;0.0010 
Disease C;Expected Count;40;0.0010 
Disease D;Count;45;0.05 
Disease D;Expected Count;50;0.05 

下面的代碼生成的分組barplot:

literature.disease2 <- read.table(file = "/user/literature-disease2.csv",sep=";", header=TRUE) 
literature.disease2.sorted <- literature.disease2[order(literature.disease2$pValue,literature.disease2$Category),] 
ggplot(data=literature.disease2.sorted, aes(x=Disease, y=Value, fill=Category)) + 
geom_bar(stat="identity", position=position_dodge(),size=.3, colour="black") + 
    scale_fill_manual(values=c("Count" = "lightblue", "Expected Count" = "pink")) + # Change color 
    xlab("Disease Category") + ylab("Literature Count") + # Set axis labels 
    ggtitle("Genome-Wide Literature Counts") +  # Set title 
    theme_bw() + 
    theme(axis.text.x = element_text(angle =90, hjust = 1,vjust=0.5)) 

這是劇情: enter image description here

現在我想以色爲藍色酒吧(而不是淡藍色)和紫色(而不是粉紅色),只要pValue < = 0.005。我知道我必須更改scale_fill_manual選項,但我不知道如何對分組條形圖執行此操作。誰能幫忙?

在此先感謝,弗蘭克

+1

這是一個很好的問題。但是,我認爲你的代碼太雜亂了許多不必要的部分。儘量保持您的示例儘量少,以便其他人可以更輕鬆地理解它。 – Alex

+0

是的,你是對的。我應該專注於具體問題,以便其他人更容易遵循。 – Frank

回答

1

此代碼可能讓你開始,但考慮增加的意義,例如,阿爾法審美,而不是雙重編碼的。

關鍵是使用interaction()作爲fill美學和適當的映射顏色。

library(ggplot2) 
literature.disease2 <- read.table(text = "Disease;Category;Value;pValue 
Disease A;Count;100;0.0001 
Disease A;Expected Count;50;0.0001 
Disease B;Count;80;0.0005 
Disease B;Expected Count;40;0.0005 
Disease C;Count;60;0.0010 
Disease C;Expected Count;40;0.0010 
Disease D;Count;45;0.05 
Disease D;Expected Count;50;0.05",sep=";", header=TRUE) 
literature.disease2.sorted <- literature.disease2[order(literature.disease2$pValue,literature.disease2$Category),] 
ggplot(data=literature.disease2.sorted, aes(x=Disease, y=Value, fill=interaction(Category, pValue <= .005))) + 
    geom_bar(stat="identity", position=position_dodge(),size=.3, colour="black") + 
    scale_fill_manual(values=c("Count.FALSE" = "lightblue", "Count.TRUE" = "blue", "Expected Count.FALSE" = "pink", "Expected Count.TRUE" = "purple")) + # Change color 
    xlab("Disease Category") + ylab("Literature Count") + # Set axis labels 
    ggtitle("Genome-Wide Literature Counts") +  # Set title 
    theme_bw() + 
    theme(axis.text.x = element_text(angle =90, hjust = 1,vjust=0.5)) 

這裏是我的建議一個情節:

ggplot(data=literature.disease2.sorted, aes(x=Disease, y=Value, fill=Category, alpha = pValue <= .005)) + 
    geom_bar(stat="identity", position=position_dodge(),size=.3, colour="black") + 
    scale_fill_manual(values=c("Count" = "blue", "Expected Count" = "purple")) + # Change color 
    scale_alpha_discrete(range = c(0.5, 1)) + # Make the "insignificant" bars a little more visible (default range = c(0.1,1)) 
    xlab("Disease Category") + ylab("Literature Count") + # Set axis labels 
    ggtitle("Genome-Wide Literature Counts") +  # Set title 
    theme_bw() + 
    theme(axis.text.x = element_text(angle =90, hjust = 1,vjust=0.5)) 
+0

謝謝你的解決方案。選項「交互()」的使用是缺失的部分。 – Frank