R lattice-尝试使用y.scale.components自定义更改标签颜色
我目前正试图通过改变其Y轴标签颜色来定制一个格子平行图,这取决于这些相同标签的特征。我创建了一个定制的y.scale.components函数,如许多书籍/论坛所述。但是,将新颜色向量指定给ans$left$labels$col参数后,绘图仅使用默认颜色(黑色) 代码如下:R lattice-尝试使用y.scale.components自定义更改标签颜色,r,colors,labels,lattice,R,Colors,Labels,Lattice,我目前正试图通过改变其Y轴标签颜色来定制一个格子平行图,这取决于这些相同标签的特征。我创建了一个定制的y.scale.components函数,如许多书籍/论坛所述。但是,将新颜色向量指定给ans$left$labels$col参数后,绘图仅使用默认颜色(黑色) 代码如下: test2 <- read.table(textConnection(" species evalue l1 l2 l3 Daphnia.pulex 1.0E-6 17 41 35
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
)
欢迎任何帮助,我不理解为什么颜色分配给ans$left$LABERS$col,但所有颜色都是用黑色绘制的。我还想使用相同的colorChooser data.frame更改小提琴边框颜色,但这是另一回事…在询问Deepayan Sarkar后,ans$left$labels$col值显然在lattice执行过程中被忽略 我使用了另一种解决方案,即“scales”参数。不幸的是,我不能再依靠晶格重排序函数来按数据序列的中位数对数据序列进行重排序 对于上面提到的代码,我手动对它们进行排序,然后创建具有相应顺序的颜色向量。我不能再依赖晶格重新排序(晶格公式中的“重新排序”)。然后,我用颜色设置axix标签
scales=list(col=c(color1,color2,...))
询问软件包作者的要点。
scales=list(col=c(color1,color2,...))