R 如何防止panel.function覆盖我的设置?

R 如何防止panel.function覆盖我的设置?,r,lattice,R,Lattice,我正在尝试使用自己的颜色设置在条形图上书写文本。我可以设法更新设置,并在面板上分别写一个文本,但我找不到一种方法将两者结合起来。据我所知,panel.function覆盖了我以前的一些颜色设置。如何防止“panel.function”覆盖我的首选项 例如: #This is to extract data and colour options library(RColorBrewer) library(lattice) data(postdoc, package = "latticeExtra

我正在尝试使用自己的颜色设置在条形图上书写文本。我可以设法更新设置,并在面板上分别写一个文本,但我找不到一种方法将两者结合起来。据我所知,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"))
}))
#用于提取数据和颜色选项
图书馆(RColorBrewer)
图书馆(格子)
数据(博士后,package=“latticeExtra”)
#这就是我设置偏好的方式

myColours为了解决这个问题,您需要稍微自定义
auto.key
值。如果面板是字符串
“panel.barchart”
,则会自动执行此操作,但如果您更改面板,则会禁用此自动转换。具体来说,设置
点=假
,和
矩形=真
。这应该行得通

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"))
}))
打印(plot2