更改标签颜色我目前尝试通过更改其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,但这是另一个故事...
要求软件包作者的要点。 – 2014-03-27 21:10:29