[英]r - Add text to each lattice histogram with panel.text but has error “object x is missing”
[英]panel.text xyplot R
我正在將文本添加到格狀xyplot的不同面板中,並且想知道是否有人知道不指定ax和y坐標的方法,或者是否有類似於圖例的內容可以在其中說左上或右上等?
我之所以問是因為我想在繪圖代碼中使用scales = free,但是當我這樣做時,mytext代碼中的文本最終會覆蓋圖形的某些部分,並且不能構成一個好的繪圖。 我希望有一種方法可以繪制圖表而不進行單獨繪制,因為在我的真實數據集中,我最多可以有10個分組因子級別(代碼中的山姆)。 提供的示例並不像真實數據那樣極端。
示例數據
d_exp<-data.frame(sams=c(rep("A",6),rep("B",6),rep("C",6)),
gear=c(rep(1:2,9)),
fraction=c(.12,.61,.23,.05,.13,.45,0.3,.5,.45,.20,.35,.10,.8,.60,.10,.01,.23,.03),
interval=c(rep(c(0,10,20),6)))
d_exp<-d_exp[order(d_exp$sams,d_exp$gear,d_exp$interval),]
標尺=相同的情節。 指定了mytext x和y坐標。
mytext<-c("N=3","N=35","N=6")
panel.my <- function(...) {
panel.superpose(col=c("red","blue"),lwd=1.5,...)
panel.text(x=2.5,y=0.5,labels=mytext[panel.number()],cex=.8)
}
xyplot(fraction~interval | sams, data=d_exp,groups=gear,type="l",
scales=list(relation="same",y=list(alternating=1,cex=0.8),x=list(alternating=1,cex=.8,abbreviate=F)),
strip = strip.custom(bg="white", strip.levels = T),drop.unused.levels=T,as.table=T,
par.strip.text=list(cex=0.8),panel=panel.my)
scales = free也是如此。 文本位於奇數處,因為所有文本都具有相同的坐標。
xyplot(fraction~interval | sams, data=d_exp,groups=gear,type="l",
scales=list(relation="free",y=list(alternating=1,cex=0.8),x=list(alternating=1,cex=.8,abbreviate=F)),
strip = strip.custom(bg="white", strip.levels = T),drop.unused.levels=T,as.table=T,
par.strip.text=list(cex=0.8),panel=panel.my)
謝謝你的幫助。
您可以使用grid.text()
以與范圍無關的方式指定單位。 例如
library(grid)
panel.my <- function(...) {
panel.superpose(col=c("red","blue"),lwd=1.5,...)
grid.text(x=.5,y=.8,label=mytext[panel.number()])
}
使用grid.text
, x
和y
值默認使用npc
單位,范圍從0到1。因此x=.5
表示居中,而y=.8
表示到頂部的80%。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.