我正在尝试使用自己的颜色设置在条形图上写文字。我可以设法更新设置并单独在面板上写文本,但我找不到合并这两者的方法。据我所知,panel.function覆盖了我之前的一些颜色设置。我怎样才能防止" panel.function"从覆盖我的偏好?
例如:
#This is to extract data and colour options
library(RColorBrewer)
library(lattice)
data(postdoc, package = "latticeExtra")
#This is how I set my preferences
myColours <- brewer.pal(6,"Greys")
my.settings13 <- list(
superpose.polygon=list(col=myColours[2:5], border="transparent"),
strip.border=list(col="black")
)
#The first plot has the right settings but no text.
#The second has the text but the legend goes back to its default settings.
print(plot1<-barchart(prop.table(postdoc,margin=1), horizontal=F, ylab = "Proportion", auto.key = list(adj = 1,column=4),par.settings=my.settings13,ltext=list(1, 0.8,"0.5" ) ))
print(plot2<-barchart(prop.table(postdoc,margin=1), horizontal=F, ylab = "Proportion", auto.key = list(adj = 1,column=4),par.settings=my.settings13,panel=function(x,y,...){
panel.barchart(x,y,...)
panel.text(1,0.80,labels=c("0.5"))
panel.text(2,0.50,labels=c("0.75"))
}))
如何编写文本(如plot2中所示)但保留所有颜色设置(如图1所示)?
答案 0 :(得分:1)
要解决此问题,您需要稍微自定义auto.key
值。如果面板是字符串"panel.barchart"
,则会自动完成,但如果您更改面板,则会禁用此自动转换。具体而言,设置points=FALSE
和rectangles=TRUE
。这应该有用。
print(plot2<-barchart(prop.table(postdoc,margin=1),
horizontal=F, ylab = "Proportion",
auto.key = list(adj = 1,column=4, points=FALSE, rectangles=TRUE),
par.settings=my.settings13, panel=function(x,y,...){
panel.barchart(x,y,...)
panel.text(1,0.80,labels=c("0.5"))
panel.text(2,0.50,labels=c("0.75"))
}))
如果你想知道这发生了什么,它实际上在lattice:::bwplot.formula
函数中(这是实际完成条形图所有工作的函数)并且与{{{ 1}}变量。