使用xyplot为对数刻度轴在散点图上添加文本

时间:2014-08-16 18:18:14

标签: r lattice scatter-plot

当我尝试在对数刻度散点图中使用panel.text(或ltext)格式添加文本时,我遇到了这个问题。我知道正常情节,你会使用:

attach(mtcars) # using the mtcars as an example:

mytext<- paste("text here")


xyplot(mpg~wt, scales=list(cex=.8, col="red"),
       xlab="Weight", ylab="Miles per Gallon", 
       main="MGP vs Weight by Horse Power",
       panel=function(x, y, ...) {
         panel.xyplot(x, y, ...);
         ltext(4.5, 15, labels=mytext, cex=2)}
)

enter image description here

如果我对对数刻度图执行相同操作,则不会显示文本。

xyplot(mpg~wt, scales=list(cex=.8, col="red",
                               x = list(log = T), y = list(log = T)),
       xlab="Weight", ylab="Miles per Gallon", 
       main="MGP vs Weight by Horse Power",
       panel=function(x, y, ...) {
         panel.xyplot(x, y, ...);
         ltext(4.5, 15, labels=mytext, cex=2)}
)

enter image description here

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要将x和y坐标转换为文本的对数比例。

library(lattice)
xyplot(mpg~wt, mtcars, 
    scales=list(cex=.8, col="red", x = list(log = T), y = list(log = T)),
    xlab="Weight", ylab="Miles per Gallon", 
    main="MGP vs Weight by Horse Power",
    panel=function(x, y, ...) {
        panel.xyplot(x, y, ...);
        ltext(log10(4.5), log10(15), labels=mytext, cex=2)
    }
)

enter image description here