2013-05-03 63 views
2

更改標籤顏色我目前嘗試通過更改其Y軸標籤顏色來定製格子平行圖,具體取決於這些標籤的字符。我創建了一個定製的y.scale.components函數,如許多書籍/論壇中所述。但是,在爲ans $ left $ labels $ col參數分配新顏色矢量後,圖形只使用默認顏色(黑色)。R格 - 嘗試使用y.scale.components自定義

下面的代碼:

test2 <- read.table(textConnection(" 

     species evalue l1 l2 l3 
    Daphnia.pulex 1.0E-6 17 41 35 
    Daphnia.pulex 1.0E-10 11 30 25 
    Daphnia.pulex 1.0E-20 4 14 17 
    Daphnia.pulex 1.0E-35 4 8 15 
    Daphnia.pulex 1.0E-50 1 4 8 
    Daphnia.pulex 1.0E-75 0 2 6 
Ixodes.scapularis 1.0E-6 7 20 118 
Ixodes.scapularis 1.0E-10 6 17 107 
Ixodes.scapularis 1.0E-20 4 6 46 
Ixodes.scapularis 1.0E-35 2 3 14 
Ixodes.scapularis 1.0E-50 0 0 5 
Ixodes.scapularis 1.0E-75 0 0 2 
")->con,header=T);close(con) 

#data.frame to assign a color to the data, depending on species names on y axis 
orga<-c("Daphnia.pulex","Ixodes.scapularis") 
color<-c("cornsilk2","darkolivegreen1"); 
phylum<-c("arthropoda","arthropoda"); 
colorChooser<-data.frame(orga,color,phylum) 

#fonction for custom rendering of left y axis labels 
yscale.components.custom<-function(...) { 
    ans<-yscale.components.default(...) 
    #vector for new label colors, grey60 by default 
    new_colors<-c() 
    new_colors<-rep("grey60",length(ans$left$labels$labels)) 
    # the for() check all labels character and assign the corresponding color with the colorChooser data.frame 
    n<-1 
    for (i in ans$left$labels$labels) { 
     new_colors[n]<-as.character(colorChooser$color[colorChooser$orga==i]) 
     #got the color corresponding to the label, with the colorChooser dataframe 
    n<-n+1 
    } 
    print(length(new_colors)) 
    cat(new_colors,sep="\n") #print the content of the generated color vector 
    ans$left$labels$col<-new_colors #assign this vector to col parameter 
    ans 
} 

#plot everything 
bwplot( reorder(species,l1,median)~l1, 
     data=test2, 
     panel = function(..., box.ratio) { 
      panel.grid(h=length(colnames(cdata[,annot.arthro]))-1,v=0,col.line="grey80") 
      panel.violin(..., col = "white",varwidth = FALSE, box.ratio = box.ratio) 
      panel.bwplot(..., fill = NULL, box.ratio = .07) 
     }, 
     yscale.components=yscale.components.custom 
) 

這裏的貓()命令的輸出,包含在yscale.components.custom功能。正如你所看到的,它輸出顏色標籤的兩倍,但是分配給ans $ left $ labels $ col的矢量的長度是2.是否有第二次調用Y軸標籤顏色?它從何而來 ?

[1] 2 
darkolivegreen1 
cornsilk2 
[1] 2 
darkolivegreen1 
cornsilk2 

任何幫助是值得歡迎的,我不爲什麼已瞭解的顏色分配給ANS $左$標籤$關口,但一切都在黑色繪製。我也想改變小提琴邊框的顏色,使用相同的colorChooser data.frame,但這是另一個故事...

回答

3

在詢問到Deepayan Sarkar後,ans $ left $ labels $ col值顯然被忽略執行。

我對「尺度」參數使用了不同的解決方案。不幸的是,我不能再依靠格子重新排序功能來重新排序我的數據序列的中位數。

對於上面提到的代碼,我手動命令它們,然後用相應的順序創建一個顏色矢量。我不能依賴格子重新排序(我的格子公式中的「重新排序」)。然後,我設置了我的axix標籤顏色

scales=list(col=c(color1,color2,...)) 
+0

要求軟件包作者的要點。 – 2014-03-27 21:10:29