panel.text xyplot R
panel.text xyplot R
我正在向格子中的 xyplot 的不同面板添加文本,想知道是否有人知道不指定 x 和 y 坐标的方法,或者是否有类似于图例的东西,你可以说左上角或右上角等?
我问是因为我想在绘图代码中使用 scales=free,但是当我执行 mytext 代码中的文本时,最终会覆盖图形的一部分并且无法绘制出好的绘图。我想有一种方法可以在不单独绘制图表的情况下绘制图表,因为在我的真实数据集中,我有多达 10 个分组因子级别(代码中的 sams)。提供的示例并不像真实数据那样极端。
示例数据
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)
天平=免费也是一样。文本位于奇怪的位置,因为所有文本都具有相同的坐标。
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%。
我正在向格子中的 xyplot 的不同面板添加文本,想知道是否有人知道不指定 x 和 y 坐标的方法,或者是否有类似于图例的东西,你可以说左上角或右上角等?
我问是因为我想在绘图代码中使用 scales=free,但是当我执行 mytext 代码中的文本时,最终会覆盖图形的一部分并且无法绘制出好的绘图。我想有一种方法可以在不单独绘制图表的情况下绘制图表,因为在我的真实数据集中,我有多达 10 个分组因子级别(代码中的 sams)。提供的示例并不像真实数据那样极端。
示例数据
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)
天平=免费也是一样。文本位于奇怪的位置,因为所有文本都具有相同的坐标。
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%。